Sign in to follow this  

using a custom class inside a C++ vector

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

This topic is 4711 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this