任何人都可以向我解释复杂 a之间的区别吗?和复杂 b(); ?
#include<iostream>
class Complex
{
public:
Complex()
{
std::cout << "Complex Constructor 1" << std::endl;
}
Complex(float re, float im)
{
std::cout << "Complex Constructor 2" << std::endl;
}
~Complex()
{
std::cout << "Complex Destructor" << std::endl;
}
};
int main()
{
Complex a;
std::cout << "--------------------------" << std::endl;
Complex b();
std::cout << "--------------------------" << std::endl;
Complex c(0,0);
std::cout << "--------------------------" << std::endl;
return 0;
}
输出:
Complex Constructor 1
--------------------------
--------------------------
Complex Constructor 2
--------------------------
Complex Destructor
Complex Destructor
如您所见,复杂 a;确实调用了它的默认构造函数,Complex b(); 不和复杂 c(0,0); 调用重载的构造函数。
这里发生了什么?我想,那个Complex b(); 会创建一个堆栈变量并调用它的默认构造函数来初始化它吗?