在这个程序中,“A的构造函数名为”打印了2次。我的疑问是,为什么“即A的构造”不与为什么构造函数没有被调用
A b = B::getA();
印刷而得到印有
A a;
在这两种情况下,我们正在创造一个新的对象。
这是我的计划:
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A's constructor called " << endl; }
};
class B
{
static A a;
public:
B() { cout << "B's constructor called " << endl; }
static A getA() { return a; }
};
A B::a; // definition of a
int main()
{
A a;
A b = B::getA();
return 0;
}