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;
}
DT7
  • 1,615
  • 14
  • 26
user2347839
  • 69
  • 1
  • 7

2 Answers2

2

Impossible. Constructors don't return anything, use another method as a factory method.

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

I confused your code is C++ or Java, so don't know what code I should use.

masoud
  • 55,379
  • 16
  • 141
  • 208
0

You might need to brush up your basic OO concepts. Constructors don't return anything. Well, in some sense they do return the instance of the class but you can't use any of the return types with constructors. Not even void. Why ? read THIS

Community
  • 1
  • 1
Prateek
  • 1,916
  • 1
  • 12
  • 22