On Sunday, 8 March 2015 05:23:35 UTC+2, JiiPee wrote:
On 08/03/2015 02:56, Richard Damon wrote:
class AnimalBase {
public:
virtual void speak(int);
virtual void speak(std::string);
...
};
Note that in your derived Animal class, speak is NOT a template member
function, just an ordinary member function in a template class using
the template parameter, so this is ok.
This solves one problem possibly. But.... I actually forgot to mention
another even more importan issue (and thats why I was talking about
getting the pointer to Dog type so much): namely the most important
thing is to get the value of the object and be able to use it, namely
m_whatToSpeak.
Because its in Dog-class I really need a pointer to a dog class I think.
Yes, I can cast the AnimalBase to Dog, but this is why I was asking is
it possible to get Dog pointer so I do not need to cast. But seems like
its not easily possible to achieve. I would like to call like this:
int a = farm.getAnimal(0)->m_whatToSpeak;
std::string b = farm.getAnimal(1)->m_whatToSpeak;
(or instead of fm_whatToSpeak via a function call).
I tried the visitor method somebody mentioned, but I think there is the
same proglem that AnimalBase does not know the type of m_whatToSpeak so
it cannot have a virtual function to return m_whatToSpeak as a return
value (the visitor can use it but not return it). But I ll think about
if I need it as a return value or not...maybe getting the value by
reference parameter.
The derived classes are allowed to return values of covariant return
types from virtual functions in C++:
// a class, and a subclass
class Foo {};
class Bar : public Foo {};
// covariant returning base class
class Baz
{
public:
virtual Foo* create() { return new Foo(); }
};
class Quux
: public Baz
{
public:
// Different return type, but it's allowed by the standard since Bar
// is derived from Foo
virtual Bar* create() { return new Bar(); }
};
int main()
{
Quux* tmp = new Quux();
Bar* bar = tmp->create();
}
That is not your problem. Your problem is that you want the caller to
get knowledge of the full type through base class pointer and for that
you need either double dispatch (visitor pattern) or RTTI
('dynamic_cast'/'typeid').
...
that return Dog. Is this what you meant? Where would the dog be returned?