Constructor call

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jinendrashankar
    New Member
    • Nov 2007
    • 10

    Constructor call

    Hi

    In my Code i am creating 2 object of my class and in assigentment operator
    passing arugement by reference.

    In output of my code i am getting to 2 Constructor call which right but there is 3 Distructor call which i am not able to understand why 3 Distructor call

    #include<iostre am.h>

    class A
    {
    public:
    A(){cout<<"Cons tructor of A"<<endl;}
    ~A(){cout<<"Dis tructor of ~A"<<endl;}
    A operator =(A &a){cout<<"assi genment oprator"<<endl; }


    };
    int main()
    {
    A a1,a;
    a=a1;
    return 0;
    }


    out put:

    Constructor of A
    Constructor of A
    assigenment oprator
    Distructor of ~A
    Distructor of ~A
    Distructor of ~A
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    You are returning by value in the function
    [code=cpp]
    A operator =(A &a){cout<<"assi genment oprator"<<endl; }
    [/code]

    becos of which a new object iscreated and for that a destructor is called
    Change that to
    [code=cpp]
    A &operator =(A &a){cout<<"assi genment oprator"<<endl; }
    [/code]
    and u wont see the 3rd destructor call.

    Raghuram

    Comment

    • jinendrashankar
      New Member
      • Nov 2007
      • 10

      #3
      Originally posted by gpraghuram
      You are returning by value in the function
      [code=cpp]
      A operator =(A &a){cout<<"assi genment oprator"<<endl; }
      [/code]

      becos of which a new object iscreated and for that a destructor is called
      Change that to
      [code=cpp]
      A &operator =(A &a){cout<<"assi genment oprator"<<endl; }
      [/code]
      and u wont see the 3rd destructor call.

      Raghuram

      Hi Raghuram,

      As u told that i am returning by value in the function becos of which a new object is created and for that a destructor is called.

      when ever new object is created its Constructor must be called and when object go out of scope or life its destructor must be called but in this case only destructor is called. it must call Constructor for that new object to create object as its called for 2 object of Class.

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        Irrelevant to your question, but worth mentioning.
        - <iostream>, <iostream.h>. Read any modern C++ book on why this is so.
        - Use CODE tags around blocks of code.

        Relevant:
        - How did you get the code to compile? The = operator says it returns something. Where’s the return?

        it must call Constructor for that new object to create object as its called for 2 object of Class.
        You don’t explicitly create a new object. Before you start trying to correct me, consider if you only have
        Code:
        A(int something);
        ...
        A a1(5), a(10);
        That is, your code is now amended to pass a parameter to the constructor. Are you still going to argue that the operator= creates a new object? Because, what is the value you pass in to the constructor?

        Don’t forget about the copy constructor. One of the cases the copy constructor is called, is when you return an object. The copy constructor is called to copy over the returned object back to wherever it was called.

        Comment

        • RRick
          Recognized Expert Contributor
          • Feb 2007
          • 463

          #5
          Olers1, you're right. The copy constructor is what is missing for this example. If it is added, then the constructors and destructors match.

          Also, operator= defined in this example, doesn't return anything. You need to add that. Oddly, this doesn't cause a warning.

          Add these changes and your code should work as expected.

          Comment

          Working...