-4

In C++ consider the two scenario,

  1. Returning object by reference.
  2. Returning object as it is.

In which of the above cases deep copying is done and Why?

Thanks in advance.

  • 1
    Returning a reference does no copying. Returning the object uses its copy constructor. Whether that does a deep copy depends on how it's written. – Barmar Jan 02 '15 at 06:36
  • 1
    None of the methods are, inherently, deep copying. Returning by value *can* do deep copying, if the class have a proper copy-constructor that does the actual deep copying. – Some programmer dude Jan 02 '15 at 06:36
  • @Barmar Depends on what? Can you give an example, or can you guide me on what I should read on to get a better understanding? – doggie style Jan 02 '15 at 06:40
  • 1
    It depends on whether the copy constructor and assignment operator makes copies of all the objects that it has pointers to or just copies the pointers. – Barmar Jan 02 '15 at 06:42
  • If you understand what deep copying is, it should be pretty obvious what it depends on. So maybe you need to go back to the books and study the concept. – Barmar Jan 02 '15 at 06:43
  • possible duplicate of [What is the difference between a deep copy and a shallow copy?](http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy) – Shahriar Jan 02 '15 at 06:49

2 Answers2

1

When an object is returned by reference, there is no copying of objects.

When an object is returned by value, a copy will be made. Whether that will be a shallow copy or a deep copy depends on the copy constructor.

Example 1

Simple struct:

struct Point
{
    double x;
    double y;
    double z;
};

You don't need to implement a copy constructor for such a struct. The compiler will generate a correctly working copy constructor for it.

Example 2

struct Edge;

struct Vertex
{
    std::list<Edge*> edges;
};

For Vertex, the copy constructor generated by the compiler will copy the list of Edges. This happens because the copy constructor of std::list makes a deep copy. However, the deep copy ends there. It won't create new Edge objects when making a copy of a Vertex. If that is not adequate, you'll have to implement a copy constructor for Vertex and do the right thing based on your need.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

If an object has pointers to dynamically allocated memory, and the dynamically allocated memory needs to be copied when the original object is copied, then a deep copy is required.

A class that requires deep copies generally needs:

  1. A constructor to either make an initial allocation or set the pointer to NULL.
  2. A destructor to delete the dynamically allocated memory.
  3. A copy constructor to make a copy of the dynamically allocated memory.
  4. An overloaded assignment operator to make a copy of the dynamically allocated memory.

To make a deep copy, you must write a copy constructor and overload the assignment operator, otherwise the copy will point to the original, with disastrous consequences.

Read this for more - Shallow vs. deep copying and How and When to Make Deep Copies in C++

Shahriar
  • 13,460
  • 8
  • 78
  • 95