Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I hide my implementation with interfaces and still build upon classes with inheritance?
    text
    copied!<p>I like the idea of using interfaces to hide my implementation details. I also really enjoying using inheritance as a way to build upon my previously created classes. How can I get both of these benefits to coexist together? Here is an example of my problem:</p> <pre><code>object.h class object { protected: //stuff that should be available to derived classes virtual void derivedHelper () = 0; public: //stuff that should be available to the client virtual object* create () = 0; virtual void mainTask () = 0; } object.cpp class objectPriv : public object { private: //implementation specific details and members protected: void derivedHelper () { //some stuff } public: objectPriv() { } object* create () { return(new objectPriv()); } void mainTask () { //more stuff } } superObject.h class superObject : public object { //problem #1 public: //stuff that should be available to the client virtual superObject* create () = 0; } superObject.cpp class superObjectPriv : public superObject { //problem #2 private: //the magic behind super object public: superObjectPriv() { } superObject* create () { return(new superObjectPriv()); } void mainTask () { object::mainTask(); //problem #3 object::derivedHelper(); //problem #4 //super extra stuff } } </code></pre> <p>So you can see here that this won't compile.</p> <p>I could implement the pure virtuals of object for superObject, but that defeat the purpose of deriving from object. I don't want to duplicate the implementation, I want to build on it.</p> <p>I could change superObject to derive from objectPriv, but then I would be exposing my implementation details. I want to hide everything specific about objectPriv from everybody.</p> <p>I can't think of any way to achieve this. I have a bad feeling that it might not be possible, but I have my fingers crossed that you folks will have some clever trick for me :)</p> <p>Thanks Les</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload