英雄贴:为什么初始化静态成员变量时new能够调用私有构造函数?
代码如下:
#include <iostram>
using namespace std;
// in .h
class Test
{
public:
static Test* GetInstance()
{
return m_pSelf;
}
private:
Test::Test()
{
cout << "Constructing" << endl;
}
Test* m_pSelf;
};
// in cpp
Test* Test::m_pSelf = new Test; //为什么这句会成功,请解释.
int main()
{
Test* pTest = Test::GetInstance();
}