Create object of unknown class (two inherited classes)

Posted by Paul on Stack Overflow See other posts from Stack Overflow or by Paul
Published on 2010-04-27T16:45:30Z Indexed on 2010/04/27 16:53 UTC
Read the original article Hit count: 161

Filed under:
|
|

I've got the following classes:

class A {
    void commonFunction() = 0;
}

class Aa: public A {
    //Some stuff...
}

class Ab: public A {
    //Some stuff...
}

Depending on user input I want to create an object of either Aa or Ab. My imidiate thought was this:

A object;
if (/*Test*/) {
    Aa object;
} else {
    Ab object;
}

But the compiler gives me:

error: cannot declare variable ‘object’ to be of abstract type ‘A’
because the following virtual functions are pure within ‘A’:
//The functions...

Is there a good way to solve this?

© Stack Overflow or respective owner

Related posts about c++

Related posts about class