Here is the code:
#include <iostream>
class Interface_A
{
public:virtual bool dothething () = 0;
};
class Inherit_B:public Interface_A
{
bool dothething ()
{
std::cout << "Doing the thing in B\n";
return true;
}
};
class Inherit_C:public Interface_A
{
bool dothething ()
{
std::cout << "Doing the thing in C\n";
return true;
}
};
/**This works */
Interface_A& makeBC ()
{
#ifdef make_B
return *(new Inherit_B ());
#elif make_C
return *(new Inherit_C());
#endif
}
/**This doesn't work
Interface_A makeC ()
{
#ifdef make_B
return ((Interface_A) (new Inherit_B ()));
#elif make_C
return ((Interface_A) (new Inherit_C ()));
#endif
}
*/
int main ()
{
Interface_A& obj = makeBC ();
obj.dothething();
// ultimate goal is to make a vector of type <Interface_A&>
return 0;
}
I want to ultimately create a vector of type <Interface_A&>
, but I cannot seem to find a way to do this. Creating a vector of type <Interface_A>
will also work, but as far as I understand it is not allowed by c++ to create pointer references to an abstract type.
I cannot use return type Inherit_B
because Interface_A
is being inherited by multiple classes and the active class is determined during compile-time.
I cannot use smart pointers because performance is an extremely critical aspect of the code.
How do I make a generic solution to this?