Because you are passing the classes by const reference, I assume you want to copy them. For that you will need additional virtual member function clone
which will return a pointer to a dynamically allocated copy of the object.
class func {
...
virtual func* clone() const;
...
};
class polynom: public func {
...
virtual polynom* clone() const override {
return new polynom(n_, coefs_);
}
...
};
class compfunc: public func {
...
compfunc(const func& outter, const func& inner): outer_(outter.clone()), inner_(inner.clone()) {}
...
virtual compfunc* clone() const override {
...
}
};
This way you can copy the passed objects regardless of their dynamic type.
1
solved C++: Abstract classes inheritance and construction