我有一個基類,當我調用訪問系統的構造函數並根據配置返回它的一個子類時。 主要思想和類是抽象他自己的工廠,根據需要返回到一個子類,具體取決於配置系統。在構造函數中返回子類方法
我代表我的想法到C + +,不一定要具體到他的東西一個草案,但對於其他語言如Java等的一般方法..
是可能的嗎?
實施例:
class system {
public:
Config config;
}
class Base {
public:
Base() {
if(System.getInstance().config == Config.FooMode) {
Foo foo = new Foo();
return foo;
}
};
};
class Foo: public Base {
public:
float a;
float b;
float c;
}
class Boo: public Base {
public:
float d;
float f;
float g;
}
int main() {
Point point = new Point();
return 0;
}
改爲使用[工廠方法](http://stackoverflow.com/questions/5120768/how-to-implement-the-factory-pattern-in-c-correctly)。 –
構造函數不返回C++或Java(或C#)。 –