C++ base template class virtual methods doesn't appear in derived? -
this question has answer here:
lest consider following code:
#include <iostream> template <typename t_val> // not used template argument struct foo { int x; }; template <typename t_val> struct bar { int x; }; template <typename t_value> struct { // 2 overloaded data() members: virtual void data(foo<t_value>) { std::cout << "foo empty\n"; } virtual void data(bar<t_value>) { std::cout << "bar empty\n"; } }; template <typename t_value> struct b : public a<t_value> { void data(foo<t_value>) { std::cout << "smart foo impl\n"; } }; int main(void) { b<float> theobject; bar<float> bar; theobject.data(bar); // expect virtual base call here return 0; }
the compiler message is:
tmpl.cpp: in function 'int main()': tmpl.cpp:43:14: error: no matching function call 'b<float>::data(bar<float>&)' theobject.data(bar); ^
void data(bar<t_value>)
-> void data(bar<t_value>&)
not change anything
why derived class b has no virtual void data(bar&)?
the other data
hidden.
do
template <typename t_value> struct b : public a<t_value> { using a<t_value>::data; // add line void data(foo<t_value>) { std::cout << "smart foo impl\n"; } };
Comments
Post a Comment