copy costructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Geevi
    New Member
    • Jan 2007
    • 13

    #1

    copy costructor

    Hi all,

    Differentiate between copy constructor and assignment operator?

    Explain each with an example.

    Thanks in advance....
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by Geevi
    Hi all,

    Differentiate between copy constructor and assignment operator?

    Explain each with an example.

    Thanks in advance....
    A constructor is used when a class is declared and parameters in brackets are used to initialize variables in the constructor

    Code:
    MyClass obj1("hello");
    A copy constructor uses the data already in another instance of this class to initialize the variables in the new object

    Code:
    MyClass obj1("hello");
    Myclass ojb2(obj1);
    An overloaded operator is used to copy the values in the member variables of one instance into the member variables of a second instance after both instances have been initialized

    Code:
    MyClass obj1("hello");
    MyClass obj2("banana");
    
    obj2 = obj1;
    the '=' operator must be overloaded to accomodate this assignment.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by willakawill
      the '=' operator must be overloaded to accomodate this assignment.
      Strictly speaking not quite true, the compiler will produce a default assignment operator (and copy constructor) if you don't explicitly write one. However it may not do the right thing as it just does a straight member to member copy and if some of the members are pointers with allocated memory you will end up with 2 classes both pointing to the same allocated memory. This will probably result in an exception at some point after 1 of the objects has been release (and as presumable freed the memory) when the other object tries to access it.

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Originally posted by Banfa
        Strictly speaking not quite true, the compiler will produce a default assignment operator (and copy constructor) if you don't explicitly write one. However it may not do the right thing as it just does a straight member to member copy and if some of the members are pointers with allocated memory you will end up with 2 classes both pointing to the same allocated memory. This will probably result in an exception at some point after 1 of the objects has been release (and as presumable freed the memory) when the other object tries to access it.
        Correct. So, strictly speaking, never use default constructors, copy constructors, destructors or operators. Unless, of course, you are fond of chasing random memory errors :)

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by willakawill
          Correct. So, strictly speaking, never use default constructors, copy constructors, destructors or operators. Unless, of course, you are fond of chasing random memory errors :)
          Probably very wise advice, however people generally do not write copy constructors and assignment operators for all there structures which they would have to in order to follow it.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            In general, a copy constructor, assignment operator, and destructor must be written for classes involving pointers to avoid shallow copy of pointers (as Banfa described above). I'm not sure they're 100% necessary for pointer-less classes, but if your problem specification requires special assignment, copy, etc, then it's not a bad thing to overload these operations/constructors.

            Comment

            • willakawill
              Top Contributor
              • Oct 2006
              • 1646

              #7
              Originally posted by Banfa
              Probably very wise advice, however people generally do not write copy constructors and assignment operators for all there structures which they would have to in order to follow it.
              And who are these 'people' for whom you speak? :)

              Of course I would only write a constructor, copy constructor, destructor, overloaded operator in circumstances that demand their use. Never just for the sake of it.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by Geevi
                Hi all,

                Differentiate between copy constructor and assignment operator?

                Explain each with an example.

                Thanks in advance....
                Has someone just done someone's homework?

                Comment

                • willakawill
                  Top Contributor
                  • Oct 2006
                  • 1646

                  #9
                  Originally posted by r035198x
                  Has someone just done someone's homework?
                  Dammit you're right!!
                  I usually avoid those and didn't see it.
                  Glad somebody is awake :)

                  Comment

                  • Geevi
                    New Member
                    • Jan 2007
                    • 13

                    #10
                    Thanks to all,

                    Comment

                    Working...