Skip to content

Instantly share code, notes, and snippets.

@kazmura11
Last active February 25, 2020 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazmura11/133b7d1180efa89b4e77 to your computer and use it in GitHub Desktop.
Save kazmura11/133b7d1180efa89b4e77 to your computer and use it in GitHub Desktop.
オペレーターオーバーロードのサンプル
#include <iostream>
#include <memory>
#include <string>
#include <iomanip>
class Foo {
private:
int a_;
int b_;
int c_;
public:
int getA() const { return a_; }
int getB() const { return b_; }
int getC() const { return c_; }
// ------------
// コンストラクタ
// ------------
//Foo(); // 省略
Foo(int a, int b, int c) {
a_ = a;
b_ = b;
c_ = c;
}
// ------------
// コピー
// ------------
//Foo(const Foo &rhs); // 省略
// ------------
// ムーブ
// ------------
//Foo(Foo &&rhs); // 省略
// ------------
// デストラクタ
// ------------
//~Foo(); // 省略
// ------------
// 単項演算子
// ------------
// const返しにするとムーブセマンティクスが働かないのでつけない
Foo operator-() const
{
return Foo(-a_, -b_, -c_);
}
// ------------
// 二項演算子
// ------------
// constつけるとコンパイルエラーになる書き方があるのでつけない
Foo &operator=(const Foo &rhs)
{
a_ = rhs.a_;
b_ = rhs.b_;
c_ = rhs.c_;
return (*this);
}
bool operator<(const Foo &rhs) const
{
return (a_ + b_ + c_) < (rhs.a_ + rhs.b_ + rhs.c_);
}
bool operator==(const Foo &rhs) const
{
return (a_ + b_ + c_) == (rhs.a_ + rhs.b_ + rhs.c_);
}
// const返しにするとムーブセマンティクスが働かないのでつけない
Foo operator+(const Foo &rhs) const
{
return Foo(a_ + rhs.a_, b_ + rhs.b_, c_ + rhs.c_);
}
// const返しにするとムーブセマンティクスが働かないのでつけない
Foo operator-(const Foo &rhs) const
{
return Foo(a_ - rhs.a_, b_ - rhs.b_, c_ - rhs.c_);
}
// const返しにするとムーブセマンティクスが働かないのでつけない
Foo operator*(const Foo &rhs) const
{
return Foo(a_ * rhs.a_, b_ * rhs.b_, c_ * rhs.c_);
}
// constつけるとコンパイルエラーになる書き方があるのでつけない
Foo &operator+=(const Foo &rhs)
{
a_ += rhs.a_;
b_ += rhs.b_;
c_ += rhs.c_;
return (*this);
}
// constつけるとコンパイルエラーになる書き方があるのでつけない
Foo &operator-=(const Foo &rhs)
{
a_ -= rhs.a_;
b_ -= rhs.b_;
c_ -= rhs.c_;
return (*this);
}
// constつけるとコンパイルエラーになる書き方があるのでつけない
Foo &operator*=(const Foo &rhs)
{
a_ *= rhs.a_;
b_ *= rhs.b_;
c_ *= rhs.c_;
return (*this);
}
};
void dump(const std::string &msg, const Foo &foo)
{
std::cout
<< msg << " ------ \n"
<< "\ta_: "
<< std::dec << std::setw(4) << std::right << foo.getA()
<< "\tb_: "
<< std::dec << std::setw(4) << std::right << foo.getB()
<< "\tc_: "
<< std::dec << std::setw(4) << std::right << foo.getC()
<< "\n";
}
void dump_bool(const std::string &msg, const bool isGreaterThan) {
std::cout
<< msg << " ------ \n\t"
<< std::boolalpha << isGreaterThan << '\n';
}
int main()
{
Foo foo1(10, 20, 30);
Foo foo2(20, 10, 30);
Foo foo3(40, 10, 20);
dump("foo1", foo1);
dump("foo2", foo2);
dump("foo3", foo3);
dump("operator-()", -foo3);
// いわゆる冪等性があるかの検証
dump("operator-()", -foo3);
Foo foo4 = foo1;
dump("operator=() foo4 = foo1", foo4);
dump_bool("foo1 < foo2", foo1 < foo2);
dump_bool("foo2 < foo1", foo2 < foo1);
dump_bool("foo1 = foo2", foo1 == foo2);
Foo foo5(5, 60, 10);
dump_bool("foo1 < foo5", foo1 < foo5);
dump("operator+() foo1 + foo2", foo1 + foo2);
dump("operator+() foo1 - foo2", foo1 - foo2);
dump("operator*() foo1 * foo2", foo1 * foo2);
foo1 += foo2;
dump("operator+=() foo1 += foo2", foo1);
foo1 -= foo2;
dump("operator-=() foo1 -= foo2", foo1);
foo1 *= foo2;
dump("operator*=() foo1 *= foo2", foo1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment