What is the difference between these 2 ways of declaring an object?
-
While I was reading some C++ code, I saw a code snippet which declared an object like this:
#include <string>
#include <cassert>
#include "BaseGameEntity.h"
#include "Locations.h"class State;
class Miner : public BaseGameEntity
{
private:State\* m\_pCurrentState; .....
};
Another way of declaring an object is to include the header file and then declare the object in the class:
#include <string>
#include <cassert>
#include "BaseGameEntity.h"
#include "Locations.h"
#include "State.h"class Miner : public BaseGameEntity
{
private:State m\_pCurrentState; .....
};
Yes, in the first code snippet, an object pointer is declared while in the second one, it is a normal object. Other than that,is there difference between these 2 ways of declaring an object? When should either method be used? :confused:
-
While I was reading some C++ code, I saw a code snippet which declared an object like this:
#include <string>
#include <cassert>
#include "BaseGameEntity.h"
#include "Locations.h"class State;
class Miner : public BaseGameEntity
{
private:State\* m\_pCurrentState; .....
};
Another way of declaring an object is to include the header file and then declare the object in the class:
#include <string>
#include <cassert>
#include "BaseGameEntity.h"
#include "Locations.h"
#include "State.h"class Miner : public BaseGameEntity
{
private:State m\_pCurrentState; .....
};
Yes, in the first code snippet, an object pointer is declared while in the second one, it is a normal object. Other than that,is there difference between these 2 ways of declaring an object? When should either method be used? :confused:
KaKa' wrote:
Yes, in the first code snippet, an object pointer is declared while in the second one, it is a normal object. Other than that,is there difference between these 2 ways of declaring an object?
The first code snippet, you have forward declared the class. So you don't need to include the corresponding header file. If both classes are independent one, it doesn't matter. Both code snippet work well. Well, it does matter when you have two classes which circular dependency. In that case, your second code snippet won't work. Check out this link[^] for more information. So, in summery, both code snippets are not exactly the same in all conditions. :) Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
KaKa' wrote:
Yes, in the first code snippet, an object pointer is declared while in the second one, it is a normal object. Other than that,is there difference between these 2 ways of declaring an object?
The first code snippet, you have forward declared the class. So you don't need to include the corresponding header file. If both classes are independent one, it doesn't matter. Both code snippet work well. Well, it does matter when you have two classes which circular dependency. In that case, your second code snippet won't work. Check out this link[^] for more information. So, in summery, both code snippets are not exactly the same in all conditions. :) Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.