使用类方法的模板

Templates making use of class methods

本文关键字:类方法      更新时间:2023-10-16

我有模板方法,我希望模板方法使用类中的特定方法来执行操作。有可能吗?

template<typename T>
int minus(T t1,T t2){
return t1-t2;
}

,在我的Apple对象类中,我有一种称为GetPrice()的方法我该如何结合两者。

这是正确的吗?

template<typename T>
int minus(T t1,T t2){
return t1.getPrice()-t2.getPrice();
}

为此,您可能需要一个普通功能:

template <class T>
int minus(T t1, T t2) {
    return t1 - t2;
}
int minus(const apple& t1, const apple& t2) {
    return t1.getPrice() - t2.getPrice();
}