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.