简体   繁体   English

C ++构造函数调用

[英]C++ Constructor call

I have written this small code snippet in C++, the output is also attached. 我用C ++编写了这个小代码片段,输出也附加了。 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. 据我所知,应该在第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
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;
}

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;

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. 现在让我们解释为什么在3种情况下调用复制构造函数1.当初始化对象时2.当一个对象作为参数传递给函数时3.当一个对象从函数返回时。

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 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM