C ++ constructor call

I wrote this small piece of code in C ++, the output is also attached. I do not understand why the constructor is called only once, while I see two calls to the destructor.

From what I understand, the default constructor and the overloaded assignment operator should be called on line 28.

Can someone please scroll through the 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
+3
source share
3 answers

The code you just called the copy constructor is the definition:

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

And you will see the following output:

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

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

  ABC b('b');
  ABC a;
  a = b;
+11
source
ABC a = b;

a, ! , :

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

, , , , !

operator char()
{
  return c;
}
+15

#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!)
}

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

, 3 1. 2. 3. .

If you do not specify your own copy constructor, then the compiler implements its own copy constructor, which copies the object in parts. You did not specify your own copy constructor, so you cannot track two objects created from code. Thanks

0
source

Source: https://habr.com/ru/post/1713548/


All Articles