#include <iostream>
class BarParent
{
virtual void fuz()
{
std::cout << "BarParent" << std::endl;
}
};
class BarChild : public BarParent
{
virtual void fuz()
{
std::cout << "BarChild" << std::endl;
}
};
class Foo
{
// ??BarParent bar;??
public:
Foo(BarParent bar);
};
What I seek is to store a copy of BarParent
that is passed to the constructor and let it reside in Foo
, while still calling the correct virtual function
This is an embedded application: Use of the heap is frown upon. So preferably, no heap
SUMMARY: To the best of knowledge, it cannot be done, becuase of the slicing problem (long story short the compiler cannot determine the size of generic Bar
and so on copying it type casts), so polymorphism cannot be achieved. Using templates might be a good idea, however, it defines multiple class
es Foo<typename BarType>
, as a result, doing a function
such as changeBar(BarParent)
, would not be possible since the compiler would define this as changeBar(BarType)
defined only for class Foo<Bartype>
. If someone has a better idea, please let me know.
I think i will have to go for heap, or const Barparent
and pointers. If the user const_cast
s, then he is asking for trouble, not my fault!