STL vector and your own objects

Started by
6 comments, last by Nitage 18 years, 9 months ago
so i was doing some reading on the STL vectors... I found something saying that if you use your own classes you have to overload the new operator?? actually it said that if you use custom classes that create new objects then you must overload the new operator.. I am assuming it meant to overload the new op for your class... //EX1 //in some code... std::vector<CEntity> m_vEntities; //the class example is below since the above is just a vector of already created Entities does it need to overload the new op..? //EX2 //in some code... std::vector<CEntity*> m_vpEntities; //the class example is below // couple of Q's //A///:if this is just going to store pointers to an object never create a |||||| new CEntity does it need to overload the new op? //B1//:if this is going to created a new CEntity does it need to overload |||||| the new op //B2//:assuming B1... what if it creates new CGraphic which is derived from |||||| CEntity is that going to create a problem with vector.. m_vpEntities[n] = new CEntity; -or- m_vpEntities[n] = new CGraphic;

// the class
class CEntity
{
	
public:
	CVector2D	*m_pVECPos; // creates a new CVector2D in constructor
	int		m_radius;
	bool		m_isAlive;

	CEntity(void);
	virtual ~CEntity(void);
};

thanks for any help.. .. do all the STL types require the samething? .. any other things i should know...?
Advertisement
No. Make sure you overload the copy constructor. This is especially important with non-pointer template class.

Kuphryn
Overload the copy constructor and the assignment operator if your class uses pointers internally.
Kevin.
Quote:Original post by MTclip
since the above is just a vector of already created Entities does it need
to overload the new op..?

//A///:if this is just going to store pointers to an object never create a
|||||| new CEntity does it need to overload the new op?

//B1//:if this is going to created a new CEntity does it need to overload
|||||| the new op

//B2//:assuming B1... what if it creates new CGraphic which is derived from
|||||| CEntity is that going to create a problem with vector..


Overloading new and delete allow you to customiz memory allocation. None of your examples require customized memory allocation. STL does not require customized memory allocation.

A: no
B1: no
B2: no
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote: //B2//:assuming B1... what if it creates new CGraphic which is derived from
|||||| CEntity is that going to create a problem with vector..


? WTF are the slashes for?
hahhah... yeah i was a little bored i was tring to line that stuff up .. it kept getting rid of my spaces...solution use something else

Quote:Original post by MTclip
so i was doing some reading on the STL vectors... I found something saying that if you use your own classes you have to overload the new operator??
actually it said that if you use custom classes that create new objects then you must overload the new operator..
I am assuming it meant to overload the new op for your class...


Nope. That would make things unnecessarily difficult [smile]

As the others have said... make sure you define a copy constructor, though, for every type you plan to stick into an STL container. You don't need to define a copy constructor for a class if you're always going to store raw pointers to the class objects, though.

my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
The std containers never call operator new for the stored type (they use an allocator class).

However, if you use a custom allocator with a std container, the custom allocator is not used internally by the stored objects.

This topic is closed to new replies.

Advertisement