Hello, I had created an interface to abstract a part of the source for a later extension. But what if I want to extend the derived classes with some special methods? So I have the interface here:
class virtualFoo
{
public:
virtual ~virtualFoo() { }
virtual void create() = 0;
virtual void initialize() = 0;
};
and one derived class with an extra method:
class concreteFoo : public virtualFoo
{
public:
concreteFoo() { }
~concreteFoo() { }
virtual void create() { }
virtual void initialize() { }
void ownMethod() { }
};
So I try to create an Instance of concreteFoo and try to call ownMethod
like this:
void main()
{
virtualFoo* ptr = new concreteFoo();
concreteFoo* ptr2 = dynamic_cast<concreteFoo*>(ptr);
if(NULL != ptr2)
ptr2->ownMethod();
}
It works but is not really the elegant way. If I would try to use ptr->ownMethod();
directly the compiler complains that this method is not part of virtualFoo
.
Is there a chance to do this without using dynamic_cast
?
Thanks in advance!