C++ Help: Vector.push_back of a class.
Can someone plz help me...
I have a class cEmployee, an object temp of class cEmployee, and a vector<cEmployee> vec.
Now the problem: The class cEmployee has a vector as a member variable... I put something in this vector, using variable temp, now this vector (from cEmployee class) has some data. After I use vec.push_back(temp), all my temp object is copyed in vec[0], except the vector inside the object, in vec[0] that vector is empty!! How to push_back so the vector inside object is also copyed... Someone help plz...
Sorry for my english...
You'll need to implement a copy constructor and assignment operator for cEmployee. Simple example:
class cEmployee{public: cEmployee() { } cEmployee( const cEmployee& Other ) { VectorOfSomething_ = Other.VectorOfSomething_; } cEmployee& operator = ( const cEmployee& Other ) { VectorOfSomething_ = Other.VectorOfSomething_; return *this; }private: std::vector<int> VectorOfSomething_;};
Quite hard to tell without any code.
In any case, having a vector member in a class should not force you to implement copy constructor and assignment operator, as the compiler-generated methods do pretty much the same as Richy2k showed.
There must be something else wrong.
In any case, having a vector member in a class should not force you to implement copy constructor and assignment operator, as the compiler-generated methods do pretty much the same as Richy2k showed.
There must be something else wrong.
This was the problem: There wasn't a compiler-generated copy constructor, cause there was already one made by me, where i forgot about the vector :D
Stupid me :D
Thank you for your replies...
Stupid me :D
Thank you for your replies...
Quote:Original post by visitor
In any case, having a vector member in a class should not force you to implement copy constructor and assignment operator, as the compiler-generated methods do pretty much the same as Richy2k showed.
Good point actually - I tend to write copy constructors for any object I plan to copy out of habit unless it's basic data types, so it's the conclusion I'd jumped to there.
Quote:Good point actually - I tend to write copy constructors for any object I plan to copy out of habit unless it's basic data types, so it's the conclusion I'd jumped to there.I think it's worth pointing out that writing constructors, copy constructors, copy assignment operators, and non-virtual destructors when the compiler-generated versions would suffice is (arguably) bad practice in C++.
This article has some information on the topic (about a third of the way down, in the section titled 'Can I trust the Compiler-Generated Copy Constructor and Assignment Operator?').
Quote:Original post by jykI absolutely 100% agree with that sentiment!Quote:Good point actually - I tend to write copy constructors for any object I plan to copy out of habit unless it's basic data types, so it's the conclusion I'd jumped to there.I think it's worth pointing out that writing constructors, copy constructors, copy assignment operators, and non-virtual destructors when the compiler-generated versions would suffice is (arguably) bad practice in C++.
It's a practice that went on at my workplace before I started there, and it's pretty much abolished now. The most code I've ever deleted comes from deleting unnecessarily (and often badly) implemented copy-constructors and copy-assignment operators.
What happened here even points out one of the reasons it is bad: It's easy to add an additional member variable and forget to add it to the copy-constructor or copy-assignment operator, leading to hard to track down bugs later.
If there is one member out of several that needs special copying behaviour then ideally you still don't implement the copy-constructor for that class. Use (or make) a smart class that manages the variable whose default copying behvaiour is insufficient, and use that smart class as the member of the other class instead.
In this case that very smart class is a vector, and it should have relieved you from the burden of having to implement the copy constructor and copy assignment operator at all.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement