I have an interesting problem. Consider this class hierachy:
class Base { public: virtual float GetMember( void ) const =0; virtual void SetMember( float p ) =0; }; class ConcreteFoo : public Base { public: ConcreteFoo( 'foo specific stuff here' ); virtual float GetMember( void ) const; virtual void SetMember( float p ); // the problem void foo_specific_method( 'arbitrary parameters' ); }; Base* DynamicFactory::NewBase( std::string drawable_name ); // it would be used like this Base* foo = dynamic_factory.NewBase('foo');
I’ve left out the DynamicFactory definition and how Builders are registered with it. The Builder objects are associated with a name and will allocate a concrete implementation of Base. The actual implementation is a bit more complex with shared_ptr to handle memory reclaimation, but they are not important to my problem.
ConcreteFoo has class specific method. But since the concrete instances are create in the dynamic factory the concrete classes are not known or accessible, they may only be declared in a source file. How can I expose foo_specific_method to users of Base*?
I’m adding the solutions I’ve come up with as answers. I’ve named them so you can easily reference them in your answers.
I’m not just looking for opinions on my original solutions, new ones would be appreciated.
The cast would be faster than most other solutions, however:
in Base Class add:
in each derived class: