Jump to content
  • Advertisement
Sign in to follow this  
homojedi

Should i 'new' or not

This topic is 3181 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 there just a quick question on pass by reference and value and vectors. i have 3 class's class EntityManager( Singleton ) { vector<Entity_information> entityVec; }; class Entity_information { vector<Animation_info> animationInfo; }; class Animation_Info { vector<SDL_Rect> Frames; }; now i know when dealing with vectors of classes that have vectors. its usually a good idea to send a pointer which has had memory allocated to make sure your dealing with the original address at all times. SO my question is with my code should i change all the stored vector values to new'd pointers? or because that the class thats managing them all is a singleton( EntityManager ) that it doesent matter? The only reason i havent new'd them yet is i dont want to be allocating memory if i dont need too. The reason i ask this is that my Entity_information class will hold the sprite position on screen and that value will have to be used and changed as its passed around instead of a copy..so i suppose i could just always pass those 2 values X,Y around as references...any thoughts?

Share this post


Link to post
Share on other sites
Advertisement
Sorry I can't see any pointers in your code. You should de-pseudo it a little.

As it is written the vectors will hold static instances. No dynamic allocation.

Share this post


Link to post
Share on other sites
Thats my point(not intended), at the moment im not using pointers. Should I be? and those where stripped down version's of my class's they actually look as such.

/////////////////////////////////////////////////////////////
class CEntityManager : private CSingleton_Warden
{
public:
CEntityManager(void);
~CEntityManager(void);

public:
int Load_Info( CEntity_Information entityInfo );
CEntity_Information Get_Entity( int id ){ return m_Entity_infoVec.at(id); }

private:
std::vector< CEntity_Information > m_Entity_infoVec;

};
///////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////
class CEntity_Information
{
public:
CEntity_Information( Vector2D initialPos );
~CEntity_Information(void);

public:
void Load_Information( CAnimation_Information animation_info );
CAnimation_Information GetAnimation_Info( int id ){ return m_Animation_InfoVec.at(id); }

public:
void SetTextureID( GLuint texture ){ m_TextureID = texture; }
GLuint GetTextureID( ){ return m_TextureID; }

void SetTextureDimensions( SDL_Rect rect ){ m_sdlRect_TextureSize = rect; }
SDL_Rect GetTextureDimensions( ){ return m_sdlRect_TextureSize; }

private:
std::vector< CAnimation_Information > m_Animation_InfoVec;
GLuint m_TextureID;
SDL_Rect m_sdlRect_TextureSize;

Vector2D m_2DVecPosition;
};
///////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////
class CAnimation_Information
{
public:
CAnimation_Information( const int Max );
~CAnimation_Information( );

public:
void Load_Frame( SDL_Rect Rect );
SDL_Rect GetFrame( int id ){ return m_SDLRect_FramesVec.at( id ); }

private:
std::vector< SDL_Rect > m_SDLRect_FramesVec;
int m_maxFrames;
};
///////////////////////////////////////////////////////////////

Share this post


Link to post
Share on other sites
As long as your Entity_information is not a virtual interface, or a class which privatizes it's copy constructor, or the design states that the ownership of entities is shared and not exclusive to EntityManager, I see no reason for having its elements dynamically allocated within the vector. If you want to modify the contents of the Entity_information vector you can just pass the individual elements as reference to some function, or pass the entire vector as reference.

Share this post


Link to post
Share on other sites
Using vectors is quite fine here. You just need to make sure how you handle instances of your classes when using them as parameter. For example, if you use EntityManager as parameter for a function call make the function use a reference parameter EntityManager &. This prevents the class from being copyied on the parameter stack.

If you need to store the instances in several places you should dynamically allocate the instances itself as EntityManager * and store the pointer in various places.

The bottom line is: Using vectors as attributes is ok as long as you do not copy the class instance that uses vectors as attributes.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!