C++;构造函数调用 我在C++中编写了这个小代码片段,输出也附上了。 我无法理解为什么构造函数只被调用一次,而我可以看到对析构函数进行了两次调用
据我所知,应该在第28行调用默认构造函数和重载赋值运算符 有人能解释一下吗:C++;构造函数调用 我在C++中编写了这个小代码片段,输出也附上了。 我无法理解为什么构造函数只被调用一次,而我可以看到对析构函数进行了两次调用,c++,C++,据我所知,应该在第28行调用默认构造函数和重载赋值运算符 有人能解释一下吗: 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
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 }
这是一个复制构造函数而不是赋值运算符!您可以这样重新定义它,您拥有的是编译器生成的一个:
ABC(const ABC& other)
{
c = other.c;
cout << c << " copy constructor" << endl;
}
刚才调用复制构造函数的代码,定义如下:
ABC(const ABC& a):c(a.c){
cout << "copying " << hex << &a << endl;
}
如果要调用默认构造函数,然后调用赋值运算符,则必须使用两个单独的语句:
ABC b('b');
ABC a;
a = b;
请查看您的系统的修改代码
#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!)
}
现在让我们解释一下原因
在3种情况下调用复制构造函数
1.初始化中的对象时
2.当对象作为参数传递给函数时
3.从函数返回对象时
如果您没有指定自己的复制构造函数,那么编译器将实现自己的复制构造函数,它将逐位复制对象。您没有指定自己的复制构造函数,因此无法跟踪从代码创建的两个对象。
谢谢更好的是,你可以使用一个初始化列表。是的,就像现在一样,复制构造函数在你的代码中包含一个对
c
赋值操作符的调用确保你不做转换操作符,我提到它只是为了好玩,不是为了这个例子:)构造函数标记有什么问题?
ABC(const ABC& a):c(a.c){
cout << "copying " << hex << &a << endl;
}
b
copying 0x7fffebc0e02f
0x7fffebc0e02e destructor b
0x7fffebc0e02f destructor b
ABC b('b');
ABC a;
a = b;
#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