• Advertisement
Sign in to follow this  

Why does this work?(Class prototype?)

This topic is 1402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I was wondering if anyone could tell me if I should do it this way and why it works

 

I have two classes, class A and class B in two differently headers.

 

in class A header I have

#include "b.h"
class a
{
  b testVar;
}

and in class B Header I have 

#include "a.h"
class b
{
 a testVar;
}

if I do it this way, the compiler gonna complains about alot of stuffs >.<,

but instead of including I somehow(accidently) got

class a;
class b
{
  a testVar;
}

and I was wondering how this worked? is it like extern/global variables? or is it more of a function prototype, but how would class a inside b.h, knows that it got defined inside a.h? and how come those two doesnt clash in name if it doesnt work as a prototype? 

Share this post


Link to post
Share on other sites
Advertisement

Note however forward declaration only works if the compiler doesn't need to know anything of the inner parts of that class. This means you can only forward declare pointers or references, not direct objects.

Edited by Endurion

Share this post


Link to post
Share on other sites

Note however forward declaration only works if the compiler doesn't need to know anything of the inner parts of that class. This means you can only forward declare pointers or references, not direct objects.

That what I was actually using it for, in one of my classes I had a parameter class pointer, and now I know its a "legit" thing I think I have other places where I can do similar things instead of the way I have done it untill now!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement