如何将重载方法的地址传递给模板方法

How to pass the address of the overloaded method to template method

我正在学习模板并遇到以下情况:

class ClassA {
   LPCWSTR   Name(void) {...};
   UINT      Name(LPCWSTR name) {...};
   LPCWSTR   Surname(void) {...};
   UINT      Surname(LPCWSTR name) {...};
}

class ClassB {
    template <class T>
    bool SetValue(UINT(T::*Setter)(LPCWSTR), T& object, LPCWSTR value) {
        (object.*Setter)(value)
    }
}

class ClassC : ClassB {
    SomeMethod() {
        ClassA a;
        // Compiler says it can't determine what overloaded instance to choose here.
        SetValue(&ClassA::Name, a, "Mike");
    }
}

如何传递正确的方法地址?

您可以这样指定您想要的那个:

SetValue<ClassA>(&ClassA::Name, a, "Mike");

SetValue(static_cast<UINT(ClassA::*)(LPCWSTR)>(&ClassA::Name), a, "Mike");

Demo