Questions tagged [copy-assignment]

112 questions
153
votes
3 answers

How can I make my class immune to the "auto value = copy of proxy" landmine in C++?

I have a fairly complex maths library I'm working on, and I have discovered a nasty bug when client code uses auto. Halfway through creating a minimal reproductive case to ask a question about it, I realise I can reproduce something similar using…
Ash
  • 1,806
  • 1
  • 11
  • 13
16
votes
2 answers

Deleting copy constructors and copy assignment operators. Which of them are essential?

I have a use case that my object must not be copied in any way. I have written an exaggerated complete list of copy constructor and copy assignment operator deletions below. There are so many of them that I can't make sure which ones to use, and…
15
votes
0 answers

Why are arrays not assignable in C/C++?

One can assign a struct to another, which results in copying all the values from struct to another: struct { int a, b, c; } a, b; ... a = b; But why are arrays not assignable like that: int a[3], b[3]; ... a = b; Because, strictly speaking,…
Kapichu
  • 2,416
  • 2
  • 15
  • 35
13
votes
5 answers

Why does operator = return *this?

Say I want to override the operator = so I can do something like Poly p1; // an object representing a polynomial Poly p2; // another object of the same type p2 = p1; // assigns all the contents of p1 to p2 Then in my implementation of the…
Jude Maranga
  • 644
  • 1
  • 7
  • 19
12
votes
1 answer

When is the copy assignment operator called?

When I read about copy constructor and copy assignment constructor, what I understood is that both gives away their properties one to another, and that both of them are declared implicitly by compiler (if not defined). So both must be exist whether…
Mas Bagol
  • 3,597
  • 7
  • 34
  • 63
12
votes
4 answers

C++ Object Instantiation vs Assignment

What is the difference between this: TestClass t; And this: TestClass t = TestClass(); I expected that the second might call the constructor twice and then operator=, but instead it calls the constructor exactly once, just like the first.
Andrew
  • 1,274
  • 12
  • 14
11
votes
2 answers

Is there ever a valid reason to not return *this from copy assignment operator?

Let foo be a struct or class with a copy assignment operator: struct foo { foo &operator=(const foo &); // or with some other return type? }; Is there ever a sensible reason to return anything other than *this from the operator=()? Using it…
Sami Liedes
  • 942
  • 6
  • 18
10
votes
1 answer

Default copy assignment with array members

I've got a class definition similar to the following: class UUID { public: // Using implicit copy assignment operator private: unsigned char buffer[16]; }; I've just had a unit test fail on me that was verifying that copy assignment…
Drew Hall
  • 26,700
  • 10
  • 58
  • 79
9
votes
2 answers

Overload copy assignment operator for a member struct of a non-type template struct

I have the following non-type template: template struct Path{ struct Point{ float x; float y; } }; Point segment[MAX_SIZE]; }; If I now declare two different Paths, I cannot assign elements of the…
magnetometer
  • 606
  • 4
  • 10
7
votes
1 answer

Why in C++11 or C++14 does the compiler implicitly delete the copy constructor when I declare a move assignment operator?

I wanted to create a list data structure with an iterator class in it. Everything works well but when I declare a move assignment operator the program doesn't compile if it's using the C++14 or C++11 standards, but works fine in C++17,…
6
votes
1 answer

Default constructor expression and lvalues

My C++ colleagues and I ran into a curious construct: struct A { int i; }; void foo(A const& a); int main() { foo(A() = A{2}); // Legal } The A() = A{2} expression completely befuddled us as it appears to be assigning A{2} to a temporary,…
KyleKnoepfel
  • 1,240
  • 6
  • 23
5
votes
2 answers

Is it possible to make `=` prefer assignment-from-conversion over (deleted) copy-assignment?

I've found a few threads that heavily imply this can't be done, but none use exactly the same combination of operators and conditions, so I'd like to ask more specifically. Hopefully that means it's a quick and easy answer for someone... one way or…
underscore_d
  • 5,331
  • 3
  • 29
  • 56
5
votes
2 answers

Is copy assignment operator with copy and swap idiom and self assignment check recommended?

Here you can see copy assignment operator implementation with self assignment check: String & operator=(const String & s) { if (this != &s) { String(s).swap(*this); //Copy-constructor and non-throwing swap } // Old resources…
Narek
  • 35,407
  • 69
  • 202
  • 359
5
votes
2 answers

Why do I need to delete resources when using copy-assign operator?

Code like this from one of my books for example: class HasPtr { public: HasPtr(const HasPtr& h): ps(new std::string(*h.ps)), i(h.i) { } HasPtr(const std::string &s = std::string()): ps(new std::string(s)), i(0) { } HasPtr&…
AntiElephant
  • 1,068
  • 9
  • 15
5
votes
1 answer

Can I write both copy and move assignment operators for a class?

These are my prototypes, MyClass& operator=(MyClass rhs); // copy assignment MyClass& operator=(MyClass &&rhs); // move assignment But when I call MyClass a, b; a = std::move(b); , there is an error. 556 IntelliSense: more than one operator "="…
ZHOU
  • 1,051
  • 2
  • 9
  • 23
1
2 3 4 5 6 7 8