使用类方法的模板
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();
}
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 在静态库中嵌入类方法
- 如何制作一个将函数作为参数的类方法
- 从父类方法返回子类对象
- 使用用户定义的参数调用future/async并调用类方法
- 重载类方法的不明确调用
- 单独定义模板化嵌套类方法的正确语法
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- 如何访问由共享指针保存的类方法?
- 将子类方法声明为基类的友元
- 我的模板类方法返回错误类型?
- Qt将信号与另一个类方法连接
- 我无法使用C++指针指向类方法返回的 std::vector
- 如何在成为指向基类的指针后保留对子类方法的使用?
- 检查类方法中是否(此 == nullptr)
- 从基类实例调用派生类方法而不进行强制转换
- 函数从唯一代码调用正确的子类方法
- C++ - 如何在不静态的情况下将回调绑定到类方法?
- C++ |DLL / EXE - 如何从导出的类调用另一个类方法?
- C++:从属性类调用顶级类方法