尝试创建只能在堆中实例化的类。
class Test1
{
Test1();
Test1(const Test1 &);
public:
static Test* getObject()
{
return new Test();
}
};
int main()
{
Test1* ptr1 = Test1::getObject();
}
错误:
ObjectOnHeapOnly.cpp:39:12: error: cannot initialize a variable of type 'Test1 *' with an rvalue of type 'Test *'
Test1* ptr1 = Test1::getObject();
^ ~~~~~~~~~~~~~~~~~~
1 error generated.