views:

165

answers:

4

Hello,

I am new to c++ and i have a question.

Lets say we have a base class Base and two derived classes, Derived1 and Derived2. f.e. Derived1 has a constructor taking a integer and Derived2 a constructor taking a boolean.

Is it possible to determine at run time (or at compile time) which of those two subclasses to create and assign it to the Base class.

Something like this: Base b = ???(value), where value is of type integer or boolean.

Thanks in advance!

+4  A: 

Write two overloads of a function called createMyBlaBla. One accepting int, and the other accepting bool. Everyone returns the desired derived class type. e.g.:

Base* create(int n)
{
    return new Derived1(n);
}
Base* create(bool b)
{
    return new Derived2(b);
}
....
Base* b1 = create(10);    // Derived1
Base* b2 = create(false); // Derived2

People call this the factory pattern.

AraK
+3  A: 

You probably want Factory Design Pattern.

Nikolai N Fetissov
A: 

Thanks for the answers!

What i am doing now is exactly what AraK said. I just wanted to avoid this, if there was a way to do it.

As i said im new to C++ and i dont know all possibilities of the language. I think i will keep it the way im doing it.

Thanks everybody!

George B.
Please add such comments as a *comment* to your question, not an answer. Also note, if you tell us more about the context, i.e. what your classes are supposed to do, we might be able to give you better solutions.
Georg Fritzsche
+1  A: 

I don't really think this is possible the way you want it to be, polymorphism in C++ just doesn't work like this.

If I understood well, you want a variable declared as Base decides, depending on the parameter type, whether it is going to be Derived1 or Derived2, all without using the Factory pattern.

The reason why this is not possible is that, the Base class is not really aware of the existence of its Derived classes nor you can declare a stack variable and make it "behave" as a derived class. However, I can suggest a work-around but then again, this doesn't satisfy all the expectations of the real class hierarchy you want (if you really want it that way_:

class Facade{

public:
    Facade(int foo) : b(new Derived1(foo)){}

    Facade(bool foo) : b(new Derived2(foo)){}

    Base Value()
    {
        return *b;
    }

private:
    Base* b;

};

And then you can do something like:

Facade foo(10);
Facade bar(true);

int x = (reinterpret_cast<Derived1*>(foo.Value())) -> val;
bool y = (reinterpret_cast<Derived2*>(bar.Value())) -> val;
Anzurio
By the way, reinterpret_cast is unsafe... You'd be better off using dynamic_cast instead.
Anzurio
Downcast should use static_cast if the caller is certain that the runtime type actually is the specified derived type, dynamic_cast otherwise (and check for null). You can only dynamic_cast classes with a virtual function, so make sure there is one (probably the destructor will be virtual, if you're messing with pointers and polymorphism).
Steve Jessop