template and overload function
Hi all,
I tried to create the template below :
template <class T>
class MyClass {
............
T* myFct (const CString& str) ;
.....................
}
and after i created another class that derive from the class above like :
class MyDeriveClass : public MyClass
{
........................
MyDeriveClass* myFct (bool , int, float, int, int) ; // where this myFct call the myFct of MyClass
}
could i do that ?
Because when i do that, i have a compile error !!
Thx for your ideas
[613 byte] By [
Patator] at [2007-11-20 10:15:43]

# 1 Re: template and overload function
Could i do that ?No. Eithertemplate<class T>
class MyDerivedClass<T> : public MyClass<T>orclass MyDerivedClass : public MyClass<int> // or similarBut MyClass without template parameter is not defined, so you cannot derive from it.
Not sure, what exactly you are trying with myFct. Please explain.
# 2 Re: template and overload function
No. Eithertemplate<class T>
class MyDerivedClass<T> : public MyClass<T>orclass MyDerivedClass : public MyClass<int> // or similarBut MyClass without template parameter is not defined, so you cannot derive from it.
Not sure, what exactly you are trying with myFct. Please explain.
I am tryin to do something like that :
MyDerivedClass : public MyClass<T>
{
//and here i have a function that have the same name of the function of MyClass
MyDeriveClass* myFct (bool , int, float, int, int)
{
.............
//CString toto;
myFct(toto);
}
}
and i got this error :
error C2660: MyDeriveClass::myFct' : function does not take 1 arguments
it seems that the compiler doesn't recognise the function myFct of the class MyClass.
I resolved the problem by calling it as :
MyClass<T>:: myFct(toto);
My question is: why the compiler doesn't be able to recognize the function with one parameter ?
Thanks in advance
# 3 Re: template and overload function
Derived class' member redefining the base member hide them in the derived class' scope and they become invisible there. The call resolution just looks at the names visible in the derived class' scope. It find a match which takes different number and type of parameters. And hence the error.
If by any chance you had the base and derived non-virtual members with same name and same arguments types and count - something like that would have caused an infinite recursion!
Another solution apart from the one you implied by explicitly specifying the scope of the function during the call was to put a using directive for the base member:using Base::Function;in the derived class. That brings the base function into the derived class' scope.
# 4 Re: template and overload function
:
class MyDeriveClass : public MyClass
{
........................
MyDeriveClass* myFct (bool , int, float, int, int) ; // where this myFct call the myFct of MyClass
}
Just to add to what has already been told in this thread..
You have to understand concept of template... Here MyClass is not a complete class but a template..
However, MyClass<int> becomes a complete class generated at the time of compilation..
So the bare menimum you have to do is..
class MyDerivedClass: public MyClass<int>