Jump to content
  • Advertisement
Sign in to follow this  
moucard

using a custom class inside a C++ vector

This topic is 4995 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 to everyone. I'm trying to remember what a class needs in order to be used properly as a vector object. I think it needed a copy constructor, a = operator and something else. What I want to do is simple:
class CMyObject {
  // define my object
};

..
vector<CMyObject> array;
..etc.
What are the prerequisites for CMyObject class? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by moucard
What are the prerequisites for CMyObject class? Thanks in advance.


Copy constructible and assignable. That's all.

If you have custom copy constructor and assignment operator, you generally also need a custom destructor.

Share this post


Link to post
Share on other sites
In order to be used as the held type of a standard container class, a type needs to by copy constructable and assignable. That is given an type T, and two objects t of type T and of type const T.

t = u must compile and have a return type T &
T(t) must compile
T(u) must compile
&t should yield a pointer of type T *
&u should yield a pointer of type const & *

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!