Copy Construtor

This is a discussion on Copy Construtor within the C++ Programming forums, part of the General Programming Boards category; Hi All I was wondering that why in the below code, the copy construtor is called 2 times. Code: class ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    75

    Copy Construtor

    Hi All

    I was wondering that why in the below code, the copy construtor is called 2 times.

    Code:
    class A
    {
      private:
      static int count; 
       int age;
      public:
      A() 
    { 
      cout<<"Inside A default Construtr= "<<++count<<endl;
    
    }
       A(int a)
       {
           cout<<"Inside A Parameter Construtr"<<endl;
         age = a;
       }
       A(const A&x)
       {
         cout<<"Inside Copy Construtor"<<++count<<endl;
       }
    
       
     ~A()
      {
        cout<<"Inside Destructor= "<<--count<<endl;
      }
    };
    
     A f(A x)
     {
      return x;
     }
    
    int A:: count = 0;
    
    int main()
    {
       A a;
       A b= f(a);
       return 0;
    }
    output is:

    Inside A default Construtr= 1
    Inside Copy Construtor=2
    Inside Copy Construtor=3
    Inside Destructor= 2
    Inside Destructor= 1
    Inside Destructor= 0
    I think that when f(a) is called, since I am passing this as value, no copy constructor should be called. The copy constructor should called when the return object "x" is assigned to:

    A b = x;
    Can anybody through light why copy constructor called 2 times?

    Thanks
    nickman

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Run the code with this function: (notice the ref)
    Code:
    A f(A& x)
     {
      return x;
     }
    Can you tell the difference?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    75
    Thanks.

    But I want to know, why in my case copy constructor is called twice. Since I was simply returning the object and copy construtor should called on:

    A b = x;

    Thanks for your time.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    You did that:
    Code:
    A b= f(a);
    So, when f(a) was called, what happened to a? Moreover, look at the definition of the function named "f()". The argument is neither a reference nor a pointer. So, we have to copy the a from main to the argument of your function. So, copy constructor was called.
    The second call comes from the assign operator, but I think you had figured that already.

    When you use the function with the reference, the copy constructor get's called once, because we pass a reference in the function to the already constructed object a.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    75
    So it means, when ever we pass object as value and not reference, a new object is created which called copy constructor.
    But I am curious to know why only copy constructor is called and not the normal constructor?
    Is it by design that only copy constructor got called when object pass as value to function?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    But, imagine that when the function gets called, the object comes from main and gets assigned to the argument of the function, thus we can imagine it as an assignment in that case, which of course calls the copy constructor and not the real constructor.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    75
    Thanks Again.

    Again I think that if I do this ( as you are saying its assignment):

    A c;
    c = a;
    Then again copy constructor should called, but it doesn;t.
    The reason what I think is, what in f() happening is:

    f( A x = a)
    which is calling copy constructor.

    Am I right?

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    If I understand correct, you mean this:
    Code:
    int main()
    {
       A a;
       A c;
       c = a;
       return 0;
    }
    In this case, no copy constructor is called, because both 'a' and 'c' objects are already constructed.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Quote Originally Posted by std10093 View Post
    The second call comes from the assign operator, but I think you had figured that already.
    No, first call comes from copying a to the function.
    Second call comes from returning a local variable by value.
    Remove the assignment and it will still be the same.

    Quote Originally Posted by nickman View Post
    So it means, when ever we pass object as value and not reference, a new object is created which called copy constructor.
    Correct.

    But I am curious to know why only copy constructor is called and not the normal constructor?
    Think about it: a copy constructor is also a constructor which constructs an object by copying over the state from another object.
    A default constructor is a constructor that constructs an object with an empty state.

    Is it by design that only copy constructor got called when object pass as value to function?
    Yes, due to the above.

    Quote Originally Posted by nickman View Post
    Thanks Again.

    Again I think that if I do this ( as you are saying its assignment):

    Then again copy constructor should called, but it doesn;t.
    That is assignment which means that the copy constructor should not be called. Instead, the copy assignment operator is called.
    You should separate

    A a;
    A b(a); // Or A b = a;

    and

    A a, b;
    a = b;

    The former is copy construction and the later is copy assignment.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Quote Originally Posted by Elysia View Post
    No, first call comes from copying a to the function.
    Second call comes from returning a local variable by value.
    Remove the assignment and it will still be the same.
    Oh Elysia is correct. How in the world did I miss that? Sorry.

    If you run this code, you will see what Elysia says:
    Code:
    A& f(A& x)
     {
      return x;
     }
     
    int A:: count = 0;
     
    int main()
    {
       A a;
       f(a);
       return 0;
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to copy
    By pode in forum C++ Programming
    Replies: 5
    Last Post: 11-20-2002, 03:59 AM
  2. fd copy
    By mary in forum C Programming
    Replies: 12
    Last Post: 07-18-2002, 06:22 PM
  3. just a copy ?
    By black in forum C++ Programming
    Replies: 6
    Last Post: 05-14-2002, 08:04 AM
  4. copy?
    By Kavity in forum C++ Programming
    Replies: 6
    Last Post: 12-30-2001, 01:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21