How to use both native and native copy constructor in C ++?

I have a long class with a lot of participants. I want to write a copy constructor for it. But if I write my own copy constructor, I have lost access to the deafult copy constructor.

I just want to repair some pointers in my own copy constructor. Therefore, I want to have a shallow copy of the object, which can be executed by default.

Is it possible to access the default copy constructor when there is already my own copy constructor?

+8
c ++ constructor copy-constructor deep-copy shallow-copy
source share
8 answers

Wrap things that you do not want to change in the structure, and receive (in private) from it. In your copy constructor, just call the copy constructor of your base class.

+11
source share

No, you cannot have both a default and your own c-tor instance.

But there are two problems with this problem:


1 Inclusion of some pointers in some class with specific copy semantics

Example:

class A { public: private: int trivial1; int trivial2; ... SomePointer nontrivialMember; }; class SomePointer { public: SomePointer(const SomePointer&); // here the non trivial part of A copy semantics int* nonTrivialMember; }; 

2 Enclose trivial parameters in some trivial structure

Example:

 class A { public: A(const A& o) : data(o.data) { // non trivial part } private: struct Data { int trivial1; int trivial2; ... } data; int* nontrivialMember; }; 

I would always choose the first solution.

[UPDATE]

There is also a 3rd solution, very similar to my second, embedding your trivial part in a private inherited base class. I would prefer a 1st solution.

+6
source share

The simplest approach to this would be to wrap pointers to classes that will perform a β€œrepair” manually in their copy constructor, then you can happily use the default copy constructor.

+3
source share

No, there is no way to call the default copy constructor from a custom copy constructor.

0
source share

You can use the default or your own, not both. If you want to choose different functionality for different objects, you just have to write a member function that handles this case.

 void DeepCopy(MyClass* rhs); 

For example.

0
source share

You cannot access the default ctor copy if you created your own - the compiler just does not generate it. But this workaround is to split your class into data structure and logic.

See an example:

 struct Data { int i; std::string s; Data(): i(), s() {} }; class Code: private Data { public: Code() {} Code(const Code& rhs): Data(rhs) // Call default copy ctor { i = 42; // Your copy part return *this; } }; 
0
source share

My solution is a simple memcpy () instead of an impossible call to an implicit (compiler generated) copy instance as an example shown below:

 Class Foo { public: ... Foo (Foo & other) { // copies trivial part (and non-trivial part with possible wrong values) memcpy(this, &other, sizeof(Foo)); // your non-trivial part here, overwrites the wrong values (if any) above. } } 

However, a side effect is that memcpy () will also copy this non-trivial part, which is waste. If the non-trivial part does not contain too much space, I will prefer my solution.

For example, a class like below only spends 4 bytes of a copy of one pointer if the size of the pointer is 4 bytes.

 Class Bar { int x, y, z; // memcpy() wastes these 4 bytes copy, // since actual copy constructor wants a new string string *s; } 
0
source share

This worked for me ... (C ++ 11, I don't know if it works on the old std) Not sure why this doesn't end with an infinite loop.

 class Foo { public: Foo(const Foo &orig) { *this = orig; ... exchange pointers, do own stuff } 
0
source share

All Articles