tags:

views:

55

answers:

4

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 happening 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.

A: 

it looks like Base::foo() is private.

Steve Townsend
These are structs. Default access for struct is public.
Danvil
ah yes. -1 for myself. What is the compiler error?
Steve Townsend
+1  A: 

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;
}
Glen
+3  A: 

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.

Sean
+3  A: 

Either add a using declaration like the other answers show or make a call using a qualified name

e.Base<float>::foo();
Johannes Schaub - litb