这可能是以前有人问过的问题,但我找不到...
我在文件中有一个类.hpp:
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
}
模板方法应该适用于 int, double... 但不适用于字符串。因此,在我的.hpp文件中,我定义了func对数字的作用,并在我的.cpp文件中写道:
template<>
void A::func(std::string t){ // do something in that case}
但是当我将函数func与 一起使用时std::string,程序会调用数字的方法......所以我将.hpp文件替换为:
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
}
我的.cpp文件变成了:
void A::func(std::string t){ // do something in that case}
然后一切正常!
我的问题是,这是正确的方法吗?