3/18 Rookie C++ question: Is the following circular situation allowed -
defining a ClassA with a member variable that is a pointer to an
object of ClassB, and also defining ClassB with a member variable that
is pointer to an object of ClassA. If it is allowed, how do I go
about with the #include, #ifndef, etc.
\_ not to forget to mention that many data-structs use this
circular pointer of classes of different types (red-black trees)
so it wouldn't make sense to design one of the most versatile
languages without this feature.
\_ Yes, it's allowed. You need to do a forward declaration for the
classes that haven't been defined at the point where you're using
them. For example:
// A.h
class ClassB;
No preprocessor pain is necessary. This works for reference members
as well. And they don't have to be in separate files.... This works
too:
class ClassA { ClassB *pB; };
// B.h
class ClassA;
Good luck... -mogul
class ClassB { ClassA *pA; };
No preprocessor pain is necessary. This works for reference
members as well. And they don't have to be in separate files....
This works too:
class ClassB;
class ClassA { ClassB *pB; };
class ClassB { ClassA *pA; };
Good luck... and sign your frickin' name, punk! -mogul
\_ Hehehe! Thanks for the answer! It's kind of embarassing
to put my name when it's such a dumb question. I know
there is function declaration, but didn't know about
class declaration, having always used the #include, #ifndef
stuff.
\_ real men use "typename" for forward decls. or something. -ali
\_ Whatever. Real men know when to use typename. And that's in
template functions and classes when the type name might not be
easily derived as such.
\_ real men use real languages that don't have this sort of BS. |