C++ class外的==重载,判断相等,测试等于,重载示例。二元操作符

#include <iostream>

// overloading "operator == " outside class
// == 是二元操作符

//////////////////////////////////////////////////////////

class Rectangle
{
public:
	Rectangle(int w, int h) 
		: width(w), height(h)
	{};

	~Rectangle() {};

	//bool operator== (Rectangle& rec);


public:
	int width;
	int height;
};

//////////////////////////////////////////////////////////

bool operator==(Rectangle & ths, Rectangle & rec)
{
	return ths.height == rec.height
		&& ths.width == rec.width;
}

//////////////////////////////////////////////////////////

int main()
{
	Rectangle a(40, 10);
	Rectangle b(40, 10);
	Rectangle c(4, 10);

	std::cout << (a == b) << std::endl;
	std::cout << (a == c) << std::endl;
	std::cout << (b == c) << std::endl;

	return 0;
}

  

posted @ 2019-12-01 09:29  路边的十元钱硬币  阅读(322)  评论(0编辑  收藏  举报