Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


#Actualzacaj

Posted 30 July 2013 - 08:50 AM

A.h:

class B; //forward declaration

class A
{
//contents
}

B.h:

class A;

class B
{
//contents
}

A.cpp:

#include "A.h"
#include "B.h"

//code 

B.cpp:

#include "B.h"
#include "A.h"

//code

This is the safest way.  Basically you can't include a file that includes the first file.  

 

Forward declaring a class allows you to define pointers using that type, but not access them.  

 

So in class A you can have

 

B* b;

 

but you can't have an inline constructor that calls a function in b or accesses its variables.

 

Since you don't usually #include a .cpp file directly, it's safe to just include all the classes .h files in your .cpp files


#1zacaj

Posted 30 July 2013 - 08:49 AM

A.h:

class B; //forward declaration

class A
{
//contents
}

B.h:

class A;

class B
{
//contents
}

A.cpp:

#include "A.h"
#include "B.h"

//code 
B.cpp:

#include "B.h"
#include "A.h"

//code

This is the safest way.  Basically you can't include a file that includes the first file.  

 

Forward declaring a class allows you to define pointers using that type, but not access them.  

 

So in class A you can have

 

B* b;

 

but you can't have an inline constructor that calls a function in b or accesses its variables.

 

Since you don't usually #include a .cpp file directly, it's safe to just include all the classes .h files in your .cpp files


PARTNERS