Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Base class redefinition in a big hierarchy

Status
Not open for further replies.

Nosferatu

Programmer
Jun 9, 2000
412
RO
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:

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
 
X-) I don't understand the situation. Is the base class some sort of collection of child classes? If so, you could define some update method in the child classes.
If this is an inheritance question, the child classes could use accessor methods on their superclass to use the latest state of the superclass' properties.

Best regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top