this might be question that someone asked before but i can't find it...
i have a class in a .hpp
file :
class A{
public:
A(){//constructor}
~A(){//destructor}
//some public methods and arguments
template<typename Type>
void func(Type t){//do something for numbers}
private:
//some private methods and arguments
}
The template method should work for int, double... but not for string. Thus in my .hpp
file, I defined what func
does for numbers and in my .cpp
file I wrote :
template<>
void A::func(std::string t){ // do something in that case}
But when I use the function func
with std::string
, the program calls the methods for numbers... So I replaced the .hpp
file by :
class A{
public:
A(){//constructor}
~A(){//destructor}
//some public methods and arguments
template<typename Type>
void func(Type t){//do something for numbers}
void func(std::string s);
private:
//some private methods and arguments
}
and my .cpp
file became :
void A::func(std::string t){ // do something in that case}
and then everything works !
my question is, is this the correct way to do that ?