回复 19楼 Devil_W
怎么不可以了,上面的私有的。子类不可以访问。
而父类通过STATIC 来进行构造。
那里不可以了,,,
用心做一件事情就这么简单
#include "StdAfx.h" #include <iostream> using namespace std; class A { public: static A* f() { return new A; } private: A() { cout<<"construct"<<endl; } }; class B:public A { public: B() { cout<<"B constrcut"<<endl; } }; int main() { cout << "Hello world!" << endl; //A d(0,0); A *a = A::f(); //B c; return 0; }这样可以实现符合你的要求了吧。。。。
#include <iostream> using namespace std; class A { public: static A* f() { return new A; } private: A() { cout<<"construct"<<endl; } }; class B:public A { public: B():A(*A::f()) { cout<<"B constrcut"<<endl; } }; int main() { cout << "Hello world!" << endl; B b(); return 0; }
#include <iostream> using namespace std; class A { public: static A* f() { return new A; } private: A() { cout<<"construct"<<endl; } A(A&) { cout<<"不给你调用复制构造函数"<<endl; } }; class B:public A { public: B():A(*A::f()) { cout<<"B constrcut"<<endl; } void print() { cout<<"nihao"<<endl; } }; int main() { cout << "Hello world!" << endl; B b; b.print(); return 0; }