Determine an object's class returned by a factory method (Error: function does not take 1 arguments

Posted by tzippy on Stack Overflow See other posts from Stack Overflow or by tzippy
Published on 2013-10-18T09:38:34Z Indexed on 2013/10/18 9:54 UTC
Read the original article Hit count: 147

Filed under:
|
|

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);
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about inheritance