Chapter 51

2.1.1 【C++】operator

ひえひえ
ひえひえ
2023.01.15に更新
このチャプターの目次

概要

operatorについて確認する

参考

http://wisdom.sakura.ne.jp/programming/cpp/cpp27.html

内容

構造体とかクラスで比較演算や演算子による加算減算などを簡単にできる方法という解釈をしてます。

コード例

struct Member
{
	int num;
	std::string name;

	Member()
		:num(0)
		,name("")
	{}

	Member(int nu, std::string na)
		:num(nu)
		, name(na)
	{}

	bool operator==(const Member mem)
	{
		bool is_hit = false;
		if (num != 0)
		{
			is_hit = num == mem.num;
		}
		else
		{
			is_hit = name == mem.name;
		}
		return is_hit;
	}
	bool operator!=(const Member mem)
	{
		bool is_hit = false;
		if (num != 0)
		{
			is_hit = num == mem.num;
		}
		else
		{
			is_hit = name == mem.name;
		}
		return !is_hit;
	}
};