Image

Imagelupinehunter wrote in Imagecpp

Class hierarchy question

I have two classes A and B, both have own .h and .cpp files.
.h files are in the form:

#ifndef A_H
#define A_H
//defn of class A
#endif

Both of the classes have a pointer member variable that points to the other.(A has a "B* b;" and vice versa)
Now how I normally get it to compile is, put a dummy "class A;" in B.h before B's definition and vice versa. (Is this the right way?)
Then it complies right and works.

But when I want to derive classes from these, things get messy. Say I want to derive C from B, C has its own .h and .cpp files, and C.h has those ifdefs. C.h would look like this:

#ifndef C_H
#define C_H
#include "B.h"

class C: public B
{ //...
};

#endif

But this won't compile since B.h is already considered by compiler and B_H is #define'd. Like I do above, I put a class B; before C's definition, but then the compiler says base class B has incomplete type and does not compile.

What is the good way of dealing with such situations? Can't I have two classes that have pointers to each other and then derive another class from one of those? I know I can do that if I put them in one file, but I want seperate files for classes.

Thanks in advance.