Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default assignment operator= in c++ is a shallow copy?

Just a simple quick question which I couldn't find a solid answer to anywhere else. Is the default operator= just a shallow copy of all the class' members on the right hand side?

Class foo { public:   int a, b, c; };  foo f1, f2; ... f1 = f2; 

would be identical to:

f1.a = f2.a; f1.b = f2.b; f1.c = f2.c; 

This seems to be true when I test it but I need to be sure I'm not missing some specific case.

like image 892
Christopher Dorian Avatar asked Feb 23 '11 20:02

Christopher Dorian


People also ask

Does assignment operator do shallow copy?

Like the copy constructor, the assignment operator has to make a copy of an object. The default version makes a shallow copy. If a deep copy is desired for assignments on a user-defined type (e.g. a class), then the assignment operator should be overloaded for the class.

Is there a default assignment operator?

If a class definition does not declare a parameterless constructor, a copy constructor, a copy assignment operator, or a destructor, the compiler will implicitly declare them. These are called default operators. A C-like struct has these default operators.

What is deep copy and shallow copy in C?

A shallow copy in this particular context means that you copy "references" (pointers, whatever) to objects, and the backing store of these references or pointers is identical, it's the very same object at the same memory location. A deep copy, in contrast, means that you copy an entire object (struct).

Are copy constructor deep or shallow?

The default copy constructor and default assignment operators do shallow copies, which is fine for classes that contain no dynamically allocated variables. Classes with dynamically allocated variables need to have a copy constructor and assignment operator that do a deep copy.


2 Answers

I'd say, default operator= is a copy. It copies each member.

The distinction between a shallow copy and a deep copy doesn't arise unless the members being copied are some kind of indirection such as a pointer. As far as the default operator= is concerned, it's up to the member being copied what "copy" means, it could be deep or shallow.

Specifically, though, copying a raw pointer just copies the pointer value, it doesn't do anything with the referand. So objects containing pointer members are shallow-copied by default operator=.

There are various efforts at writing smart pointers that perform clone operations on copying, so if you use those everywhere in place of raw pointers then the default operator= will perform a deep copy.

If your object has any standard containers as members, then it may be confusing to (for example) a Java programmer to say that operator= is a "shallow copy". In Java a Vector member is really just a reference, so "shallow copy" means that Vector members aren't cloned: source and destination refer to the same underlying vector object. In C++ a vector member will be copied, along with its contents, since the member is an actual object not a reference (and vector::operator= guarantees the contents are copied with it).

If your data member is a vector of pointers, then you don't have either a deep copy or a shallow copy. You have a semi-deep copy, where the source and destination objects have separate vectors, but the corresponding vector elements from each still point to the same, uncloned object.

like image 73
Steve Jessop Avatar answered Sep 19 '22 09:09

Steve Jessop


Yes, default operator= is a shallow copy.

By the way, the actual difference between shallow copy and deep copy becomes visible when the class has pointers as member fields. In the absence of pointers, there is no difference (to the best of my knowledge)!

To know the difference between them, see these topics (on stackoverflow itself):

  • What is the difference between a deep copy and a shallow copy?
  • Deep copy vs Shallow Copy
like image 38
Nawaz Avatar answered Sep 20 '22 09:09

Nawaz