Jump to content
  • Advertisement
Sign in to follow this  
Sillencer

C++ Help: Vector.push_back of a class.

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

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

Share this post


Link to post
Share on other sites
Advertisement
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_;
};


Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
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++.
I absolutely 100% agree with that sentiment!
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.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!