Skip to main content
gamedev.net
🔒 Locked

Template + Inheritance + Overloading

Started by snooty 4 replies 800+ views
Original Post
snooty
snooty
Hi, I run into the following problem: template<typename T> class Base { public: void foo(int i) {} }; template<typename T> class Derived : public Base<T> { public: }; Everything works fine until I overload foo(int) in Derived: template<typename T> class Derived : public Base<T> { public: void foo(double d) {} }; Now I can't call foo(int) with the Derived class. The compiler says "cannot convert parameter 1 from double to int." Any solution?
Fruny
Fruny
The int function isn't in the same scope as the double function, so you aren't overloading. The double function hides the int function. I think you also are getting hit by two-phase name lookup issues.

Add using Base<T>::foo; in Derived's body to bring the int function into Derived's scope.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
snooty
snooty
Quote:
Original post by Fruny
The int function isn't in the same scope as the double function, so you aren't overloading. The double function hides the int function.

Add using Base<T>::foo; in Derived's body to bring the int function into Derived's scope.


Thanks for the clarification. But the real problem happens outside the Derived class definition, meaning:


Derived p;
p.foo(0.5); //compiler complains
Fruny
Fruny
Derived is a class template. You can't just do Derived p;.

#include <iostream>template<typename T> class Base {public:    void foo(int i) { std::cout << "int" << std::endl; }};template<typename T> class Derived : public Base<T> {public:    using Base<T>::foo;    void foo(double) { std::cout << "double" << std::endl; }};int main(){    Derived<char> p;    p.foo(0);    p.foo(0.5);}


Works perfectly for me on GCC 3.4.5.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
snooty
snooty
Quote:
Original post by Fruny
Derived is a class template. You can't just do Derived p;.


Right, that was a typo.

Quote:
Original post by Fruny
using Base<T>::foo;
void foo(double) { std::cout << "double" << std::endl; }


Here, does it mean foo is overloaded?
I'm not familiar with the using keyword.
Fruny
Fruny
Quote:
Original post by snooty
Here, does it mean foo is overloaded?


Yes.

Quote:
I'm not familiar with the using keyword.


Well, then it's the perfect opportunity. Consult your favourite C++ reference and familiarize yourself with it. [smile]


(And to think that some people believe C++ is good for beginners...)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.