Determine an object's class returned by a factory method (Error: function does not take 1 arguments
- by tzippy
I have a factorymethod that either returns an object of baseclass or one that is of derivedclass (a derived class of baseclass). The derived class has a method virtual void foo(int x) that takes one argument. baseclass however has virtual void foo() without an argument.
In my code, a factory method returns a pointer of type bar that definetly points to an object of class derivedclass. However since this is only known at runtime I get a compiler error saying that foo() does not take an argument.
Can I cast this pointer to a pointer of type derivedclass?
std::auto_ptr<baseclass> bar = classfactory::CreateBar(); //returns object of class derivedclass
bar->foo(5);
class baseclass
{
public:
virtual void foo();
}
class derivedclass : public baseclass
{
public:
virtual void foo(int x);
}