Skip to main content
GameDev.net gamedev.net
🔒 Locked

using a custom class inside a C++ vector

Started by moucard Jan 18, 2005 at 12:10 PM 1 replies 500+ views
Original Post
moucard
moucard
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.
Fruny
Fruny
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
SiCrane
SiCrane
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 & *

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.