2

I have a base class which when I call constructor which accesses the system and depending on the configuration and return me a subclass of it. The main idea and the class was abstract his own factory, and returns to a sub-class as needed, depending on the config system.

I represented my idea into a draft in C + +, not necessarily to be something specific to him, but for a general approach for other language like java, etc..

is possible?

Example:

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;
}
4

2 回答 2

2

不可能的。构造函数不返回任何东西,使用另一种方法作为工厂方法。

class Base
{
  Foo makeFoo()
  {
  }
  ...
};

我混淆了你的代码是 C++ 还是 Java,所以不知道我应该使用什么代码。

于 2013-10-30T18:27:15.397 回答
0

您可能需要复习基本的 OO 概念。Constructors不要返回任何东西。好吧,从某种意义上说,它们确实返回了instanceof,class但您不能将任何return类型与constructors. 甚至没有void。为什么 ?读这个

于 2013-10-30T18:28:52.087 回答