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…
![](../../users/profiles/2555705.webp)
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…
![](../../users/profiles/245376.webp)
hkBattousai
- 9,613
- 17
- 66
- 114
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,…
![](../../users/profiles/2979958.webp)
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…
![](../../users/profiles/4826123.webp)
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…
![](../../users/profiles/4019871.webp)
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.
![](../../users/profiles/8871.webp)
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…
![](../../users/profiles/1073784.webp)
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…
![](../../users/profiles/23934.webp)
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…
![](../../users/profiles/3982107.webp)
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,…
![](../../users/profiles/13924902.webp)
Kanony
- 462
- 12
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,…
![](../../users/profiles/3585575.webp)
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…
![](../../users/profiles/2757035.webp)
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…
![](../../users/profiles/163394.webp)
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&…
![](../../users/profiles/3348786.webp)
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 "="…
![](../../users/profiles/1351406.webp)
ZHOU
- 1,051
- 2
- 9
- 23