Should i 'new' or not

Started by
3 comments, last by Waterwalker 14 years, 4 months ago
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?
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.
[size="2"]I like the Walrus best.
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;
};
///////////////////////////////////////////////////////////////
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.
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.
------------------------------------I always enjoy being rated up by you ...

This topic is closed to new replies.

Advertisement