Jump to content
  • Advertisement
Sign in to follow this  
MTclip

STL vector and your own objects

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

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...?

Share this post


Link to post
Share on other sites
Advertisement
No. Make sure you overload the copy constructor. This is especially important with non-pointer template class.

Kuphryn

Share this post


Link to post
Share on other sites
Overload the copy constructor and the assignment operator if your class uses pointers internally.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

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!