I wonder about an elegant way to solve an inheritance problem, with its implementation done in C++.
What I would like to do is to have a hierarchy in which the base class plays an important role in determing
some of the components of the children classes. I want to do this in the base class to avoid trivial
methods to be implemented in each of the derived classes (about 18 of them), which is really mind numbing.
Here is a short example:
Now there is the content change problem and reusing the code.
If I were to implement getContent into derived classes, I would have no problem in modifying its behavior by defining a derived class etc...
What I expect is that ALL the child classes content will change at once, so I would have to modify them all, if I would use this approach.
So, what I want to do is derive a new base class for the hierarchy and let the hierarchy know that its base class changed. Is there a way to do this (without using templates)?
One wild guess would be using macros definitions for the base class of ChildXXX. Then, when deriving the new base class, one would undefine the symbol defined for the base class and redefine it as pointing to the new base class.
Something like:
Does anybody ever used such a thing?
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
What I would like to do is to have a hierarchy in which the base class plays an important role in determing
some of the components of the children classes. I want to do this in the base class to avoid trivial
methods to be implemented in each of the derived classes (about 18 of them), which is really mind numbing.
Here is a short example:
Code:
class BaseClass {
...
protected:
getContent();
};
class Child1 /*,Child2,Child3 etc... */: public BaseClass {
public:
int process();
};
int ChildXXX::process() {
getContent(); // call to the base class method, instead of implementing getContent in each of the derived // classes.
}:
Now there is the content change problem and reusing the code.
If I were to implement getContent into derived classes, I would have no problem in modifying its behavior by defining a derived class etc...
What I expect is that ALL the child classes content will change at once, so I would have to modify them all, if I would use this approach.
So, what I want to do is derive a new base class for the hierarchy and let the hierarchy know that its base class changed. Is there a way to do this (without using templates)?
One wild guess would be using macros definitions for the base class of ChildXXX. Then, when deriving the new base class, one would undefine the symbol defined for the base class and redefine it as pointing to the new base class.
Something like:
Code:
#define _defined_BaseClass BaseClass // base class above
class Child1 /*,Child2,Child3 etc... */: public _defined_BaseClass {
public:
int process();
};
//change the base class behavior:
class RedefinedBaseClass:public BaseClass {
getContent();
...
};
// have then the macro definition
#ifdef _defined_BaseClass
#undef _defined_BaseClass
#endif
#define _defined_BaseClass RedefinedBaseClass
Does anybody ever used such a thing?
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro