lava
lava

Reputation: 2045

C++ Constructor call

I have written this small code snippet in C++, the output is also attached. I fail to understand why the constructor is being called only once, while i can see two calls being made for destructor.

From what i understand, default constructor and overloaded assignment operator should be called at line 28.

Can someone please throw some light on this:

  1 #include <iostream>
  2 using namespace std;
  3 
  4 class ABC {
  5   char c;
  6   public:
  7     ABC() {
  8       cout << "default" << endl;
  9     }
 10     ABC(char c) {
 11       this->c = c;
 12       cout << c << endl;
 13     }
 14     ~ABC() {
 15       cout << hex << this << " destructor " << c << endl;
 16     }
 17     void method() {
 18       cout << "method" << endl;
 19     }
 20     void operator= (const ABC& a) {
 21       cout << "operator" << endl;
 22     }
 23
 24 };
 25 
 26 int main() {
 27   ABC b('b');
 28   ABC a = b;
 29 }

Output in g++ version 4.0.1:
~/src$ g++ test.cpp
~/src$ ./a.out 
b
0xbffff0ee destructor b
0xbffff0ef destructor b

Upvotes: 3

Views: 5877

Answers (3)

Tunvir Rahman Tusher
Tunvir Rahman Tusher

Reputation: 6631

Have a look to the modified code of your's

#include <iostream>
using namespace std;
class ABC {
    char c;
public:

    ABC() {
        cout << "default" << endl;
    }
        ABC(char c)
        {
            cout<<"parameterized constructor called\n";/////overloaded constructor called for the first line in main
            this->c = c;
            cout << c << endl;
        }
        ABC(ABC &c)
        {
            cout<<"Copy cons\n";//copy constructor is called for the second line in main
        }


        ~ABC() {
            cout << hex << this << " destructor " << c << endl;
        }
        void method() {
            cout << "method" << endl;
        }
        void operator= (const ABC& a) {

        }


    };


int main()
{
        ABC b('b');//////here parameterized constructor is called i.e <ABC(char c)>
        ABC a = b;/////////Here the copy constructor is called not the default one.(total 2 object created so the destructor is called twice!)
}

The output of the program is

parameterized constructor called
b
Copy cons
0x7fff5fbff820 destructor �
0x7fff5fbff828 destructor b

Now lets explain why copy constructor is called in 3 cases 1.When an object in initialized 2.When an object is passed as parameter to a function 3.when an object is returned from a function.

If you not specify your own copy constructor then compiler implement its own copy constructor which copy the object bit by bit. You have not specify your own copy constructor thats why you can not track the two object created from the code. Thanks

Upvotes: 0

Arpegius
Arpegius

Reputation: 5887

The code you have just call the copy constructor, this is the definition:

ABC(const ABC& a):c(a.c){
    cout << "copying " << hex << &a << endl;
}

And you shoud see output like this:

b
copying 0x7fffebc0e02f
0x7fffebc0e02e destructor b
0x7fffebc0e02f destructor b

If you want to call default constructor and then the assignment operator you must use two separate statement:

  ABC b('b');
  ABC a;
  a = b;

Upvotes: 11

Khaled Alshaya
Khaled Alshaya

Reputation: 96859

ABC a = b;

This is a copy constructor not the assignment operator! you could redefine it like this what you have is compiler-generated one :

ABC(const ABC& other)
{
 c = other.c;
 cout << c << " copy constructor" << endl;
}

If you really insist on not using a copy constructor you can add converstion operator like to your class and forget the copy constructor!

operator char()
{
  return c;
}

Upvotes: 15

Related Questions