Igor Tandetnik wrote:
"Daniel Ranger" <dranger003@gmail.com> wrote in message
news:1146328499.034351.171590@j33g2000cwa.googlegroups.com
Hi, I have an issue where I have two classes that require a complete
declaration of each other in order to compile and I can't get it to
work. Here's my code:
class Obj1 {
public:
Obj2 m_obj2;
}
class Obj2 {
public:
Obj1 m_obj1;
}
Forward declaration or otherwise, this is not going to work. Ask
yourself - what should sizeof(Obj1) be? It must be large enough to hold
an instance of Obj2, which should be large enough to hold Obj1, which
should be ...
As stated above, I meant to declare a reference pointer to the other
object, not an instance of the object.
What I have done is forward declare the namespace & classes and now it
works. I have been reading quite a few other posts about similar issues
and found that there are the ones that forward declare & the ones that
use header files.
I guess forward declare is nice because you bring your compile time
down when modifying your code.
On the other side, forward decalre is redundant...
Which method do you use in instances like above? Or in any situations?
Thanks,
Dan.
Always use forward declaration if you can. A header file should include
everything it needs to compile against an empty .cpp file, and nothing more.