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