0

Let's suppose I have the following code:

std::vector<myclass> v1;
v1.push_back (x1);
v1.push_back (x2);
std::vector<myclass> v2(v1);

When I run the code above, the copy constructors of both x1 and x2 are called to build the vector v2, which seems the right thing to do.

But if I rewrite the code in this way:

std::vector<myclass> v1, v2;
v1.push_back (x1);
v1.push_back (x2);
v2 = v1;

the copy constructors of x1 and x2 are called again. Is it the right behaviour? Why is not the assignment operator called? Wouldn't it be more consistent to call the assignment operator of x1 and x2? It seems that a copy assignation of a vector doesn't mean a copy assignment of its elements.

What if I have a class where copy construction and copy assignation must have different semantics?

EDIT. v2 is empty before the call to v2=v1, but shouldn't the copy assignment be called after creating a vector with 2 elements? It would seem more consistent to me... BTW, the compiler forces the definition of a myclass copy assignment operator when I try to call v2=v1, even when it isn't using it.

jbgs
  • 2,795
  • 2
  • 21
  • 28
  • Different semantics? Like what? – Oliver Charlesworth Mar 30 '13 at 02:21
  • 1
    Anyway. Copy-assignment would require a `myclass` instance to copy it to. `v2` is an empty vector... – Oliver Charlesworth Mar 30 '13 at 02:24
  • E.g. ownership of shared resources. Maybe it is desided to transfer the ownership when the copy assgnment is called, but not when copy construction is called. – jbgs Mar 30 '13 at 02:28
  • That sounds like that would be a dangerously inconsistent design... – Oliver Charlesworth Mar 30 '13 at 02:29
  • Well, if copy constructor and copy assignment are supposed to be exactly the same thing, why do we have both? Anyway, it would be nice to know which one containers are using in the background. – jbgs Mar 30 '13 at 02:38
  • Because you need both in order to achieve consistent semantics for your class (read about the [Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three), etc.) – Oliver Charlesworth Mar 30 '13 at 02:40
  • Rule of Three says that if my class needs either a copy constructor, an assignment operator, or a destructor, then it is likely to need all three of them; not that copy constructor and assignment operator should exhibit the same behaviour. Anyway, I agree that it's not the best design. But I still think that it's a good idea to know which one is gonna be called. – jbgs Mar 30 '13 at 02:45
  • 1
    C++03 standards requires that the type of objects stored in stl containers must meet the requirements of CopyConstructible types (20.1.3), and the additional requirements of Assignable types (23.1). Thats why compiler forces the definition of a copy assignment operator. – Amar Mar 31 '13 at 05:09

1 Answers1

1

It doesn't make sense to call the assignment operator for x1 and x2 because you aren't assigning to either of them. v2's assignment operator will make its internal array large enough for all the elements of v1 and then copy all of the elements of v1 into v2.

Scott Olson
  • 3,513
  • 24
  • 26