使用场景
当函数对象之间需要进行转换时,可以通过构造合适的转换方法——也就是适配器来实现
实现代码
#include<iostream>
#include<vector>
using namespace std;
struct Point{ int x, y; };
struct Line { Point start, end; };
class LineToPointAdapter {
vector<Point> points;
public:
virtual vector<Point>::iterator begin() { return points.begin(); }
virtual vector<Point>::iterator end() { return points.end(); }
LineToPointAdapter(Line& line) {//转换过程由构造函数完成,是饿汉式
//change line to points
}
};
//“懒汉式”是在你真正用到的时候才去建这个单例对象:
//“饿汉式”是在不管你用的用不上,一开始就建立这个单例对象
int main() {
return 0;
}
特点
通过设置一个类,其中的函数用来实现类与类之间转换。