boost operator 实例代码

// BoostOperator.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <boost/operators.hpp>
using namespace boost;
class Point :public less_than_comparable < Point >,\
	equality_comparable<Point>,
	addable<Point>
{
public:

	Point(int a, int b) :a(a), b(b) 
	{
	}
	friend bool operator<(const Point &p, const Point &pp)
	{
		return p.a < pp.a;
	}
	friend bool operator==(const Point &p, const Point &pp)
	{
		return p.b == pp.b;
	}
	friend Point operator+=( Point &p, const Point &pp)
	{
		p.b += pp.b;
		return p;
	}
public:
	int a;
	int b;
	
};
int _tmain(int argc, _TCHAR* argv[])
{
	Point p1(2, 4),p2(4,3);
	std::cout<<(p1<p2 )<< endl;
	std::cout << (p1>p2) << endl;
	std::cout << (p1!=p2) << endl;
	std::cout << (p1 += p2).b << endl;
	return 0;
}


发布了136 篇原创文章 · 获赞 22 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/u010261063/article/details/81043689