Questions tagged [copy-assignment]

146 questions
4
votes
2 answers

Implementing valuelike copy-assignment operator

In C++ Primer there is an example of using the copy-control members to make a class act "valuelike"; that is, when copying the object, the copies are independent. It proposes the following code: class HasPtrValue { public: HasPtrValue(const…
JamesLens
  • 447
  • 6
  • 14
4
votes
2 answers

Will objects be copied in assignment in D?

When I assign an object in D, will it be copied? void main() { auto test = new Test(new Object()); tset.obj; } class Test { public Object obj; public this(Object ref origObj) { obj = origObj; // Will this copy origObj into…
Jeroen
  • 15,257
  • 12
  • 59
  • 102
4
votes
1 answer

In C++, does a constructor that takes the base class count as a copy constructor?

For example: class Derived : public Base { Derived(const Base &rhs) { // Is this a copy constructor? } const Derived &operator=(const Base &rhs) { // Is this a copy assignment operator? } }; Does the…
Matt
  • 21,026
  • 18
  • 63
  • 115
3
votes
3 answers

parameter pack templated constructor deletes copy assignment

Trying to understand why having a parameter pack templated constructor for a class apparently causes both the copy constructor and the copy assignment operator to be optimized out. (Actually I can see how the compiler wouldn't be able to discern…
3
votes
1 answer

Effect of user deleted auto constructors on implicit generation of copy constructors

How does user deleted auto constructors affect implicit generation of copy constructors? For eg: struct Foo { Foo(X) = delete; auto operator=(X) = delete; }; int main() { Foo a; Foo b(a); b = a; } In the above struct, if X is…
3
votes
0 answers

In C++, placement new and copy constructor implementing copy assignment operator, is it a good practice?

Given the code struct Foo{ Foo(const Foo &other){ i = other.i; }; Foo &operator=(const Foo &other){ if(this == &other){ return (*this); } new (this) Foo(other); return (*this); }; int i = 0; }; struct Bar{ …
3
votes
3 answers

Why does the address of the assigned object not change in C++?

In this C++ example, a class C has a default constructor, a copy constructor and an assignment operator: struct C { C(); C(const C& c); C& operator=(const C& c); }; The implementation is as follows with some output for tracking the…
TMOTTM
  • 3,286
  • 6
  • 32
  • 63
3
votes
2 answers

How to explicitely make a deep copy of an array in MATLAB?

For example, I want to do a deep copy of a to b: >> a=zeros(2,3); >> b=a; So here = creates only a shallow copy. My question is, how to generate a deep copy in this case? I know that I can add a command like b(1,1)=b(1,1) to make it a deep copy.…
f. c.
  • 1,095
  • 11
  • 26
3
votes
2 answers

Is assiging a shared_ptr to itself safe?

Is it safe to self-assign a std::shared_ptr? So here is an example: std::shared_ptr> pVec = std::make_shared>(); std::cout << pVec.use_count() << std::endl; // 1 pVec = pVec; I know that assigning…
Itachi Uchiwa
  • 3,044
  • 12
  • 26
3
votes
3 answers

C++ declare a move/copy operation will suppress generation of related operations?

I saw a sentence in The C++ programing language which I'm confused with: • If the programmer declares a copy operation, a move operation, or a destructor for a class,no copy operation, move operation, or destructor is generated for that class. I…
Frank
  • 107
  • 6
3
votes
1 answer

fill insert() - copy constructor and copy assignment noexcept status?

Are STL container elements required to have noexcept copy-constructors and copy-assignment operators? Please provide a reference if possible. If not, what is the state of a STL container when an exception happens during a multi-insert, e.g. during…
dhke
  • 15,008
  • 2
  • 39
  • 56
3
votes
5 answers

Why should the parameter to a copy assignment operator should be a reference?

"opperator= should takes a parametor of the (of course,const best) ref of src obj",i see this in many books,but i try to use non-ref instead,it also works!so,whats the purpose of using ref?is it just to avoid copy from the param? my test code…
aishuishou
  • 83
  • 7
3
votes
1 answer

Assigning array carrying a reference to a new array permits writing to the reference without accessing the original variable assigned to the reference

$a = [1 => "A"]; $b = &$a[1]; $c = $a; $c[1] = "C"; echo $a[1]; Output : C (But I am expecting the output to be A) Apparently, array is not referenced by = sign. $c = $a; <- this should make a copy of $a and assign it to $c, but why does reference…
user2816220
  • 43
  • 1
  • 3
3
votes
1 answer

Automation between containers of different objects with copy assign

I have a sparse vector of type std::vector> where SparseElement is: template struct SparseElement { I index; T value; //............ SparseElement &operator=(const…
Chameleon
  • 1,804
  • 2
  • 15
  • 21
2
votes
2 answers

C++ How to store object in arrays without them deleted

I want to seek help on this issue I encountered when learning C++. I tried to store objects into an array directly, but realize the objects gets deconstructed right away. I could not figure out why exactly is this so. #include class…
1 2
3
9 10