Function overloading with polymorphic templates
Why is the following code not compiling and how would it be possible to use the function from the base class?
template<typename K> struct Base
{
K foo() { return (K)0; }
};
template<typename K> struct Extension
: public Base<K>
{
K foo(int a) { return (K)a; }
};
int main()
{
Extension<float> e;
e.foo();
return 0;
}
Edit: Ok, I thought that this is only hap开发者_运维技巧pening with template classes ... What is the idea behind the design decision to hide the base class version by its overloaded version from the child class? I mean, declaring both functions in the same class works just fine.
Extension::foo
is hiding Base::foo
. You can use a using delaration to bring it back:
template<typename K> struct Extension
: public Base<K>
{
using Base<K>::foo;
K foo(int a) { return (K)a; }
};
Item #33 ("Avoid hiding inherited names") in Scott Meyers's "Effective C++" is about this issue.
Either add a using declaration like the other answers show or make a call using a qualified name
e.Base<float>::foo();
The foo from Extension is hiding the foo from Base.
Adding a using clause to Extension solves the compilation error.
template<typename K> struct Base
{
K foo(void) { return (K)0; }
};
template<typename K> struct Extension
: public Base<K>
{
using Base<K>::foo;
K foo(int a) { return (K)a; }
};
int main()
{
Extension<float> e;
e.foo();
e.foo(1);
return 0;
}
it looks like Base::foo() is private.
精彩评论