Questions tagged [copy-assignment]
146 questions
5
votes
2 answers
C++ vector copy assignment, calling which copy mechanism of its elements?
My class A explicitly implements both its copy constructor and its copy assignment.
Which copy mechanism is used when copy assigning a vector of such elements?
Is this:
vector a1, a2(5);
a1 = a2;
going to use A's copy constructor for all new…

Gauthier
- 40,309
- 11
- 63
- 97
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
- 6,309
- 3
- 38
- 64
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
- 38,779
- 79
- 233
- 389
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,227
- 10
- 18
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,151
- 2
- 12
- 27
5
votes
3 answers
Could I have copy constructor for subclass of QObject?
Here we can read that no copy construct and copy assignment operator evaluable. But here we can read that qRegisterMetaType and Q_DECLARE_METATYPE have to have public default constructor, public copy constructor and public destructor. The question…

VALOD9
- 566
- 2
- 6
- 22
5
votes
5 answers
Why not only one? Copy constructor and assignment operator
I understand which is invoked in what situation...
Sample a;
Sample b = a; //calls copy constructor
Sample c;
c = a; //calls assignment operator
My question is Why these 2 different things exist at all? Why can't only one of the two take…

NightFurry
- 358
- 1
- 17
5
votes
1 answer
Default copy behavior with array of objects
If I have a class with an array as a member:
class A
{
Object array[SIZE];
};
And I copy an instance of it:
A a;
A b = a;
A c;
c = a;
will array be memcpy-ed byte-by-byte or Object::operator= copied element-by-element?

John
- 7,301
- 2
- 16
- 23
4
votes
3 answers
Object assignment in Ruby
Coming from a c++ background I'm curious about object assignment in Ruby. What considerations (if any) should be made for the following object assignments:
class MyClass
attr_accessor :a, :b
def initialize(a, b)
@a = a
@b = b
end
…

SundayMonday
- 19,147
- 29
- 100
- 154
4
votes
1 answer
Why is assigning a container's element to the container (not) a well-defined C++?
In C++ there is the infamous problem of self-assignment: when implementing operator=(const T &other), one has to be careful of the this == &other case to not destroy this's data before copying it from other.
However, *this and other may interact in…

yeputons
- 8,478
- 34
- 67
4
votes
2 answers
Nim sequence assignment value semantics
I was under the impression that sequences and strings always get deeply copied on assignment. Today I got burned when interfacing with a C library to which I pass unsafeAddr of a Nim sequence. The C library writes into the memory area starting at…

Stefan Zobel
- 3,182
- 7
- 28
- 38
4
votes
3 answers
Understanding and using a copy assignment constructor
I'm trying to understand how the copy assignment constructor works in c++. I've only worked with java so i'm really out of my waters here. I've read and seen that it's a good practice to return a reference but i don't get how i should do that. I…

Stelios Papamichail
- 955
- 2
- 19
- 57
4
votes
3 answers
shallow copy in python
I am a little confused on how shallow copy works, my understanding is when we do new_obj = copy.copy(mutable_obj) a new object is created with elements of it still pointing to the old object.
Example of where I am confused -
## assignment
i = [1, 2,…

Ani Menon
- 27,209
- 16
- 105
- 126
4
votes
5 answers
Implement copy-ctor in terms of copy-operator or separately?
This is not a duplicate of Implementing the copy constructor in terms of operator= but is a more specific question. (Or so I like to think.)
Intro
Given a (hypothetical) class like this:
struct FooBar {
long id;
double valX;
double valZ;
…

Martin Ba
- 37,187
- 33
- 183
- 337
4
votes
3 answers
Compiler won't use copy assignment instead move?
I have a class where the move assignment is explicit deleted, since the object should not be moveable. But if i assign to an instance of this class using RVO the compiler gives me the error:
main.cpp:12:16: note: candidate function has been…

user1810087
- 5,146
- 1
- 41
- 76