Let's say I have a template:
template <class N, class I>
void add(N* element, std::list<N*> & container, I (N::*f)() const,
std::string successmsg, std::string exceptmsg) {
//...
}
And I want to call it for a list of Base Class pointers to a derivative class.
add(newAirplane, airplanes, &Airplane::getRegistration,
"Added!", "Error: Existent!");
Airplane
inherits from AirplaneType
.
Of course, it doesn't compile, N is first defined as AirplaneType
and then as Airplane
.
I added a virtual getRegistration
@ AirplaneType but of course, the compiler gives out a vtable error.
What's the proper way to solve this? AirplaneType
has no registration
attribute and I'm not interested in it having one. I also wanted to avoid virtual getRegistration() const {return "";}
Any suggestions for good practice?
EDIT:
Thanks for answers, but still not working. I think I have found the remaining problem, but not its solution:
void Airline::addAirplane(AirplaneType* airplane) {
add(newAirplane, airplanes, &Airplane::getRegistration,
"Added!", "Error: Existent!");
}
The type of pointer received is AirplaneType
, not Airplane
.
airplanes
is a list of AirplaneType
pointers.