c++ - 多态中的函数对象
问题描述
我想在多态中实现功能对象,如下所示:
#include <algorithm>
#include <iostream>
using namespace std;
struct Compare {
virtual bool operator() (int, int) const = 0;
};
struct Less : public Compare {
bool operator() (int i, int j)
const {
return (i < j);
}
};
struct Greater : public Compare {
bool operator() (int i, int j)
const {
return (i > j);
}
};
void f(const Compare& c) {
int arr[10] = { 4,2,6,7,1,3,5,9,8,0 };
sort(arr, arr + 10, c);
for (int i = 0; i < 10; ++i)
cout << arr[i] << " ";
}
int main()
{
f(Less());
f(Greater());
}
但它有一条错误消息“没有重载函数“排序”的实例与参数列表匹配”
我认为抽象类不能有实例。我该如何解决?
解决方案
推荐阅读
- hive - Hive 提取到本地驱动器错误的输出
- ios - UITableView:滑动 RowAction 取消选中的行
- react-native - 可触摸不透明度打破居中
- java - Spring Websockets:如何验证消息和接收客户端?
- php - 使用路由器重定向登录
- spring-boot - 未从 Spring Security Oauth2 中的授权代码中获取访问令牌?调用访问令牌失败给 ERR_TOO_MANY_REDIRECTS?
- r - R - 根据另一个数据框的多列中的值之间的关系创建具有值的矩阵
- unity3d - Unity3D获取OVRCameraRig的位置
- java - Spring Security 中如何让访客在不登录的情况下访问主页
- javascript - 如何使用 PHP 获得与我需要的完全相同的 JSON 格式?