CPP Output Question - related to copy-constructor and return by value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sanjay

    #1

    CPP Output Question - related to copy-constructor and return by value

    Hi All,

    I have a doubt in understanding the output of the following program
    that i executed on my system. I was using DevC++ IDE which uses minGW
    based compiler.

    ----------------------------------------------
    #include <iostream>
    using namespace std;

    class Integer //: public DataType
    {
    private:
    int x;
    public:
    Integer(int xx = 0);
    Integer(const Integer&);
    ~Integer();
    Integer operator+(const int i);
    void operator=(const Integer& i);
    };

    Integer::Intege r(int xx) : x(xx)
    {
    cout<<"Integer( int) x is "<<x<<endl;
    }
    Integer::Intege r(const Integer& i) : x(i.x)
    {
    cout<<"Integer( Integer&)"<<end l;
    }
    Integer::~Integ er()
    {
    cout<<"~Integer ()"<<endl;
    }
    Integer Integer::operat or+(const int i)
    {
    cout<<"operator + called i is "<<i<<endl;
    Integer xx(x+i);
    return xx;
    }
    void Integer::operat or=(const Integer& i)
    {
    cout<<"operator = called i.x is "<<i.x<<end l;
    x = i.x;
    }

    int main()
    {
    Integer i1(40);
    Integer i3=i1+10; //Doubt understanding this
    return 0;
    }
    -----------------------------------
    Output:

    Integer(int) x is 40
    operator+ called i is 10
    Integer(int) x is 50
    ~Integer()
    ~Integer()
    -------------------------------------
    I have a doubt in understanding the execution of operator+.

    Inside operator+, xx is created and is returned by value. While
    returning the copy-constructor of Integer should get invoked because
    the output of operator+ is being assigned to i3 which is not yet
    constructed, but the actual output observed is different.

    Is this any kind of optimization being performed by the compiler. Is
    there anything that i am missing.

    I would appreciate if someone can explain this.

    Regards
    Sanjay Raghani
  • Victor Bazarov

    #2
    Re: CPP Output Question - related to copy-constructor and returnby value

    sanjay wrote:
    I have a doubt in understanding the output of the following program
    that i executed on my system. I was using DevC++ IDE which uses minGW
    based compiler.
    >
    ----------------------------------------------
    #include <iostream>
    using namespace std;
    >
    class Integer //: public DataType
    {
    private:
    int x;
    public:
    Integer(int xx = 0);
    Integer(const Integer&);
    ~Integer();
    Integer operator+(const int i);
    void operator=(const Integer& i);
    As a side note, operator= *usually* returns a reference to the same
    object. It's not required, of course.
    };
    >
    Integer::Intege r(int xx) : x(xx)
    {
    cout<<"Integer( int) x is "<<x<<endl;
    }
    Integer::Intege r(const Integer& i) : x(i.x)
    {
    cout<<"Integer( Integer&)"<<end l;
    You misrepresent the signature of your copy c-tor. The argument is a
    reference to a const integer, so I'd expect the output statement to be

    cout << "Integer(Intege r const&)" << endl;

    <g>
    }
    Integer::~Integ er()
    {
    cout<<"~Integer ()"<<endl;
    }
    Integer Integer::operat or+(const int i)
    {
    cout<<"operator + called i is "<<i<<endl;
    Integer xx(x+i);
    return xx;
    }
    void Integer::operat or=(const Integer& i)
    {
    cout<<"operator = called i.x is "<<i.x<<end l;
    x = i.x;
    }
    >
    int main()
    {
    Integer i1(40);
    Integer i3=i1+10; //Doubt understanding this
    return 0;
    }
    -----------------------------------
    Output:
    >
    Integer(int) x is 40
    operator+ called i is 10
    Integer(int) x is 50
    ~Integer()
    ~Integer()
    -------------------------------------
    I have a doubt in understanding the execution of operator+.
    >
    Inside operator+, xx is created and is returned by value. While
    returning the copy-constructor of Integer should get invoked because
    the output of operator+ is being assigned to i3 which is not yet
    constructed, but the actual output observed is different.
    The compiler is *allowed* to skip creation of a temporary and construct
    the result of the right-hand side expression *directly* into the object
    being constructed. Essentially, since the compiler sees the code for
    all your functions, it can pass the reference to the constructed object
    (in your case named 'i3') to the operator+ and inside the function
    construct the actual 'i3' instead of 'xx'. No temporaries, no copies.
    Is this any kind of optimization being performed by the compiler. Is
    there anything that i am missing.
    Yes, it is, and yes, you probably are.
    I would appreciate if someone can explain this.
    I hope I have.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • sanjay

      #3
      Re: CPP Output Question - related to copy-constructor and return byvalue

      On Oct 9, 5:59 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
      sanjay wrote:
      I have a doubt in understanding the output of the following program
      that i executed on my system. I was using DevC++ IDE which uses minGW
      based compiler.
      >
      ----------------------------------------------
      #include <iostream>
      using namespace std;
      >
      class Integer //: public DataType
      {
      private:
      int x;
      public:
      Integer(int xx = 0);
      Integer(const Integer&);
      ~Integer();
      Integer operator+(const int i);
      void operator=(const Integer& i);
      >
      As a side note, operator= *usually* returns a reference to the same
      object. It's not required, of course.
      >
      };
      >
      Integer::Intege r(int xx) : x(xx)
      {
      cout<<"Integer( int) x is "<<x<<endl;
      }
      Integer::Intege r(const Integer& i) : x(i.x)
      {
      cout<<"Integer( Integer&)"<<end l;
      >
      You misrepresent the signature of your copy c-tor. The argument is a
      reference to a const integer, so I'd expect the output statement to be
      >
      cout << "Integer(Intege r const&)" << endl;
      >
      <g>
      >
      >
      >
      }
      Integer::~Integ er()
      {
      cout<<"~Integer ()"<<endl;
      }
      Integer Integer::operat or+(const int i)
      {
      cout<<"operator + called i is "<<i<<endl;
      Integer xx(x+i);
      return xx;
      }
      void Integer::operat or=(const Integer& i)
      {
      cout<<"operator = called i.x is "<<i.x<<end l;
      x = i.x;
      }
      >
      int main()
      {
      Integer i1(40);
      Integer i3=i1+10; //Doubt understanding this
      return 0;
      }
      -----------------------------------
      Output:
      >
      Integer(int) x is 40
      operator+ called i is 10
      Integer(int) x is 50
      ~Integer()
      ~Integer()
      -------------------------------------
      I have a doubt in understanding the execution of operator+.
      >
      Inside operator+, xx is created and is returned by value. While
      returning the copy-constructor of Integer should get invoked because
      the output of operator+ is being assigned to i3 which is not yet
      constructed, but the actual output observed is different.
      >
      The compiler is *allowed* to skip creation of a temporary and construct
      the result of the right-hand side expression *directly* into the object
      being constructed. Essentially, since the compiler sees the code for
      all your functions, it can pass the reference to the constructed object
      (in your case named 'i3') to the operator+ and inside the function
      construct the actual 'i3' instead of 'xx'. No temporaries, no copies.
      >
      Is this any kind of optimization being performed by the compiler. Is
      there anything that i am missing.
      >
      Yes, it is, and yes, you probably are.
      >
      I would appreciate if someone can explain this.
      >
      I hope I have.
      >
      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask
      Thanks...

      Comment

      Working...