Questions tagged [copy-assignment]
146 questions
161
votes
4 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,956
- 1
- 14
- 15
23
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…

hkBattousai
- 10,583
- 18
- 76
- 124
18
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
- 865
- 2
- 9
- 27
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
- 3,346
- 4
- 19
- 38
14
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,343
- 12
- 14
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
- 4,377
- 10
- 44
- 72
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
- 1,084
- 8
- 19
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
- 28,429
- 12
- 61
- 81
9
votes
3 answers
free(): double free detected in tcache 2 in C++
Firstly, I really checked if there is a question already been asked but I could not find any. Error message should not deceive you my situation is a bit different I guess or I am just missing something.
While I was dealing with a toy C++ code I…

eminomur
- 95
- 1
- 3
- 8
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
- 646
- 4
- 12
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,…

Kanony
- 509
- 2
- 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,…

KyleKnoepfel
- 1,426
- 8
- 24
5
votes
2 answers
Would a derived class ever have an implicit copy constructor or assignment operator when it's deleted in the base class?
Qt defines Q_DISABLE_COPY as follows:
#define Q_DISABLE_COPY(Class) \
Class(const Class &) = delete;\
Class &operator=(const Class &) = delete;
Q_DISABLE_COPY is used in the QObject class, but the documentation for it says that it should be…

Joseph Sible-Reinstate Monica
- 45,431
- 5
- 48
- 98
5
votes
3 answers
In C++, can a class with a const data member not have a copy assignment operator?
I'm designing a class that ought to have a const data member called K. I also want this class to have a copy assignment operator, but the compiler seems to implicitly delete the copy assignment operator from any class with const data members. This…

Dimitrije Kostic
- 265
- 1
- 8
5
votes
2 answers
Allocator propagation policies in your new modern C++ containers
What is the reason for having these traits in a container (https://en.cppreference.com/w/cpp/memory/allocator_traits)
propagate_on_container_copy_assignment Alloc::propagate_on_container_copy_assignment if present, otherwise…

alfC
- 14,261
- 4
- 67
- 118