Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Deep copy and Shallow copy w.r.t Copy Constructor and Assignment Operator

Deep copy and Shallow copy w.r.t Copy Constructor and Assignment Operator

Scheduled Pinned Locked Moved C / C++ / MFC
helpalgorithmstutoriallearning
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hrishiS
    wrote on last edited by
    #1

    Hi to Everyone. Its been a long time, I have been searching for an clear cut idea of ....shallow copy and deep copy with regards to Copy constructor and assignment operator. I search the web, but many places has been confused me...I know that the problem comes only when we do use of pointers...But, dont know exact situation and solution for it. when to use which one..ie in which situation to used which one.... Can anyone please help me understanding the concept better. A good example problem with relating to all these topic would be appreciated. May be could you please relate the example with the following situations 1. Default copy constructor 2. Default assignment operator 3. overloaded copy constructor 4. overloaded assignment operator Thanks in advance

    ----------------------------- I am a beginner

    C 1 Reply Last reply
    0
    • H hrishiS

      Hi to Everyone. Its been a long time, I have been searching for an clear cut idea of ....shallow copy and deep copy with regards to Copy constructor and assignment operator. I search the web, but many places has been confused me...I know that the problem comes only when we do use of pointers...But, dont know exact situation and solution for it. when to use which one..ie in which situation to used which one.... Can anyone please help me understanding the concept better. A good example problem with relating to all these topic would be appreciated. May be could you please relate the example with the following situations 1. Default copy constructor 2. Default assignment operator 3. overloaded copy constructor 4. overloaded assignment operator Thanks in advance

      ----------------------------- I am a beginner

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      There's no difference concerning this behavior between a copy constructor and an assignement operator, so my explanation is valid for both. The problem with a default constructor is that it simply copies byte by byte all the content of your class. This is ok for most of the cases when your class contains only plain data type for instance. Now, if you have a class which contains a pointer and then if you copy your class using a shalow copy, both class instances will contain a pointer pointing at the same memory location. If you have a destructor like this:

      MyClass::~MyClass()
      {
      if (myPointer)
      {
      delete myPointer;
      myPointer = NULL;
      }
      }

      Then you will end up in big troubles once one of the class instances gets destroyed because its destructor will be called and you will end up with a pointer pointing to memory which has been release in your other instance. So, what you have to do instead is allocate a new pointer and copy the content when you copy the class:

      MyClass::MyClass(const MyClass& copy)
      {
      myPointer = new ....; // Allocate a new pointer
      // Copy the content of the pointer based on what is in "copy"
      ......
      }

      Cédric Moonen Software developer
      Charting control [v2.0] OpenGL game tutorial in C++

      H 1 Reply Last reply
      0
      • C Cedric Moonen

        There's no difference concerning this behavior between a copy constructor and an assignement operator, so my explanation is valid for both. The problem with a default constructor is that it simply copies byte by byte all the content of your class. This is ok for most of the cases when your class contains only plain data type for instance. Now, if you have a class which contains a pointer and then if you copy your class using a shalow copy, both class instances will contain a pointer pointing at the same memory location. If you have a destructor like this:

        MyClass::~MyClass()
        {
        if (myPointer)
        {
        delete myPointer;
        myPointer = NULL;
        }
        }

        Then you will end up in big troubles once one of the class instances gets destroyed because its destructor will be called and you will end up with a pointer pointing to memory which has been release in your other instance. So, what you have to do instead is allocate a new pointer and copy the content when you copy the class:

        MyClass::MyClass(const MyClass& copy)
        {
        myPointer = new ....; // Allocate a new pointer
        // Copy the content of the pointer based on what is in "copy"
        ......
        }

        Cédric Moonen Software developer
        Charting control [v2.0] OpenGL game tutorial in C++

        H Offline
        H Offline
        hrishiS
        wrote on last edited by
        #3

        Thanks a lot for your valuable reply Got some more doubts again.

        Cedric Moonen wrote:

        The problem with a default constructor is that it simply copies byte by byte all the content of your class.

        What does the code of default constrictor is...what I mean is what is the code present in the default constructor?

        Cedric Moonen wrote:

        Now, if you have a class which contains a pointer and then if you copy your class using a shalow copy

        Could you please elaborate this, what exactly is meant by copying using shallow copy? Does this mean copying with the default copy constructor what we have?

        ----------------------------- I am a beginner

        R C 2 Replies Last reply
        0
        • H hrishiS

          Thanks a lot for your valuable reply Got some more doubts again.

          Cedric Moonen wrote:

          The problem with a default constructor is that it simply copies byte by byte all the content of your class.

          What does the code of default constrictor is...what I mean is what is the code present in the default constructor?

          Cedric Moonen wrote:

          Now, if you have a class which contains a pointer and then if you copy your class using a shalow copy

          Could you please elaborate this, what exactly is meant by copying using shallow copy? Does this mean copying with the default copy constructor what we have?

          ----------------------------- I am a beginner

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          See Copy constructor and default constructor[^]

          “Follow your bliss.” – Joseph Campbell

          1 Reply Last reply
          0
          • H hrishiS

            Thanks a lot for your valuable reply Got some more doubts again.

            Cedric Moonen wrote:

            The problem with a default constructor is that it simply copies byte by byte all the content of your class.

            What does the code of default constrictor is...what I mean is what is the code present in the default constructor?

            Cedric Moonen wrote:

            Now, if you have a class which contains a pointer and then if you copy your class using a shalow copy

            Could you please elaborate this, what exactly is meant by copying using shallow copy? Does this mean copying with the default copy constructor what we have?

            ----------------------------- I am a beginner

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            hrishiS wrote:

            What does the code of default constrictor is...what I mean is what is the code present in the default constructor?

            If you don't provide any constructor yourself, the default constructor will simply assign all variables, similarly as you would do something like this:

            MyClass::MyClass(const MyClass& copy)
            {
            member1 = copy.member1;
            member2 = copy.member2;
            ....
            }

            So, it means that if your class contains a pointer, this will result in something like this:

            MyClass::MyClass(const MyClass& copy)
            {
            myPointer = copy.myPointer;
            }

            And of course this is not nice because both of your instances will have a pointer pointing at the same location and there's no way anymore to know which of the two has ownership of the pointer (so, which one is responsible for deleting it). And if you destroy that pointer in the class destructor, then you end up with the problem I described earlier: one instance will end up with a pointer pointing to memory which has been released (and thus which can contain corrupted data). Furthermore, you can also encounter another problem than with the one you have when manipulating pointers. If one (or more) member of your class is an object that cannot be copied (because its copy constructor has been made private for instance), then your code won't even compile because there's no way for the default constructor of your class to copy the object (as the copy constructor is private). Thus, the only way to be able to copy your class is to provide a copy constructor that instead of calling the copy constuctor of your member, it will create an new instance of this member and initialize it properly.

            hrishiS wrote:

            Could you please elaborate this, what exactly is meant by copying using shallow copy? Does this mean copying with the default copy constructor what we have?

            It is assigning all members from one class to another as I described at the begining of this message.

            Cédric Moonen Software developer
            Charting control [v2.0] OpenGL game tutorial in C++

            H 1 Reply Last reply
            0
            • C Cedric Moonen

              hrishiS wrote:

              What does the code of default constrictor is...what I mean is what is the code present in the default constructor?

              If you don't provide any constructor yourself, the default constructor will simply assign all variables, similarly as you would do something like this:

              MyClass::MyClass(const MyClass& copy)
              {
              member1 = copy.member1;
              member2 = copy.member2;
              ....
              }

              So, it means that if your class contains a pointer, this will result in something like this:

              MyClass::MyClass(const MyClass& copy)
              {
              myPointer = copy.myPointer;
              }

              And of course this is not nice because both of your instances will have a pointer pointing at the same location and there's no way anymore to know which of the two has ownership of the pointer (so, which one is responsible for deleting it). And if you destroy that pointer in the class destructor, then you end up with the problem I described earlier: one instance will end up with a pointer pointing to memory which has been released (and thus which can contain corrupted data). Furthermore, you can also encounter another problem than with the one you have when manipulating pointers. If one (or more) member of your class is an object that cannot be copied (because its copy constructor has been made private for instance), then your code won't even compile because there's no way for the default constructor of your class to copy the object (as the copy constructor is private). Thus, the only way to be able to copy your class is to provide a copy constructor that instead of calling the copy constuctor of your member, it will create an new instance of this member and initialize it properly.

              hrishiS wrote:

              Could you please elaborate this, what exactly is meant by copying using shallow copy? Does this mean copying with the default copy constructor what we have?

              It is assigning all members from one class to another as I described at the begining of this message.

              Cédric Moonen Software developer
              Charting control [v2.0] OpenGL game tutorial in C++

              H Offline
              H Offline
              hrishiS
              wrote on last edited by
              #6

              Cedric Moonen wrote:

              If you don't provide any constructor yourself, the default constructor will simply assign all variables,

              But the default constructor doesn't have any parameter with it...So in that case to which value the member variable will get initialized to?...

              Cedric Moonen wrote:

              It is assigning all members from one class to another as I described at the begining of this message.

              Does it meant that shallow copy is member to member copying of each member variable one by one...? is it,...Copy Constructor is doing shallow copy? In that case, if we dont provide an copy constructor does the default copy constructor provides deep copy?

              ----------------------------- I am a beginner

              C 1 Reply Last reply
              0
              • H hrishiS

                Cedric Moonen wrote:

                If you don't provide any constructor yourself, the default constructor will simply assign all variables,

                But the default constructor doesn't have any parameter with it...So in that case to which value the member variable will get initialized to?...

                Cedric Moonen wrote:

                It is assigning all members from one class to another as I described at the begining of this message.

                Does it meant that shallow copy is member to member copying of each member variable one by one...? is it,...Copy Constructor is doing shallow copy? In that case, if we dont provide an copy constructor does the default copy constructor provides deep copy?

                ----------------------------- I am a beginner

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #7

                hrishiS wrote:

                But the default constructor doesn't have any parameter with it...So in that case to which value the member variable will get initialized to?...

                We are talking about the default copy constructor here. The default copy constructor receives the object from which to copy.

                hrishiS wrote:

                Does it meant that shallow copy is member to member copying of each member variable one by one...? is it,...Copy Constructor is doing shallow copy?

                Yes, that's what I showed you in my previous reply: a default copy constructor simply assigns all members one by one (in fact, it is using the copy constructor of each member to create them and not the assignement operator, but this is not important in the discussion here). So, in that sense a default copy constructor is doing shallow copy.

                hrishiS wrote:

                In that case, if we dont provide an copy constructor does the default copy constructor provides deep copy?

                No, I just said the opposite: a default copy constructor does not make a deep copy. How could it do it ? You are the one how wrote the clas, so you know you have to copy some specific members (like pointers). The compiler doesn't know anything of that, he can simply assign all members from one object to the other. How can he know that for a pointer he has to allocate a new pointer and copy the content of the pointer ?

                Cédric Moonen Software developer
                Charting control [v2.0] OpenGL game tutorial in C++

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups