Jump to content
  • Advertisement
Sign in to follow this  
Weng

What is the difference between these 2 ways of declaring an object?

This topic is 3668 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

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; ..... }; What is the difference between these 2 ways of declaring an object? Yes, in the first code snippet, an object pointer is declared while in the second one, it is a normal object. When should either method be used?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Weng
Yes, in the first code snippet, an object pointer is declared while in the second one, it is a normal object.

When should either method be used?

Depends on wether the Miner OWNS the state or merely KNOWS the state which exists elsewhere.

Share this post


Link to post
Share on other sites
It might also be that State has a non-trivial constructor that so it can't be constructed when Miner is constructed (it might depend on some other data that isn't available at that point). Using a pointer will allow you to control exactly when the State object is created.

Share this post


Link to post
Share on other sites
The first version also doesn't require the full definition of State. Thus you can hide the implementation of State. This can be very useful in some situations, for example if State.h includes a lot of other headers let say from boost but you want to compile Miner to a library that doesn't require boost headers when used.
See also Opaque pointer (Pimpl idiom) .

Share this post


Link to post
Share on other sites
Quote:
Original post by Kambiz
The first version also doesn't require the full definition of State. Thus you can hide the implementation of State. This can be very useful in some situations, for example if State.h includes a lot of other headers let say from boost but you want to compile Miner to a library that doesn't require boost headers when used.
See also Opaque pointer (Pimpl idiom) .


Great explanation.

Rates++.

Share this post


Link to post
Share on other sites
Another difference is if you want to write a new state class derived from State you have to use the pointer version.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!