Skip to content

Instantly share code, notes, and snippets.

@mondoo
Last active November 10, 2023 17:08
Show Gist options
  • Select an option

  • Save mondoo/3b98bc2a8fbd5e5b14d24955d4303ae3 to your computer and use it in GitHub Desktop.

Select an option

Save mondoo/3b98bc2a8fbd5e5b14d24955d4303ae3 to your computer and use it in GitHub Desktop.
Rule of Five
class RuleOfFive
{
public:
// Default Constructor
RuleOfFive(int inInt)
{
intPtr = new int;
*intPtr = inInt;
}
// Copy Constructor
RuleOfFive(const RuleOfFive& other) noexcept
{
intPtr = new int;
*intPtr = *other.intPtr;
}
// Destructor
~RuleOfFive() noexcept
{
delete intPtr;
}
// Move Constructor
RuleOfFive(RuleOfFive&& other) noexcept
{
intPtr = new int;
*intPtr = *other.intPtr;
other.intPtr = nullptr;
}
// Copy Assignment
RuleOfFive& operator = (const RuleOfFive& rhs)
{
if (&rhs == this)
{
return *this;
}
delete intPtr;
intPtr = nullptr;
intPtr = new int(*(rhs.intPtr));
return *this;
}
// Move Assignment
RuleOfFive& operator = (RuleOfFive&& rhs) noexcept
{
if (&rhs == this)
{
return *this;
}
delete intPtr;
intPtr = rhs.intPtr;
rhs.intPtr = nullptr;
return *this;
}
// Comparison operators
// ==
bool operator == (const RuleOfFive&) const = default;
friend bool operator == (const RuleOfFive& lhs, const int rhs) { return *lhs.intPtr == rhs; }
friend bool operator == (const int lhs, const RuleOfFive& rhs) { return rhs == *rhs.intPtr; }
// !=
bool operator != (const RuleOfFive&) const = default;
friend bool operator != (const RuleOfFive& lhs, const int rhs) { return *lhs.intPtr != rhs; }
friend bool operator != (const int lhs, const RuleOfFive& rhs) { return lhs == *rhs.intPtr; }
// >
friend bool operator > (const RuleOfFive& lhs, const RuleOfFive& rhs) { return *lhs.intPtr > *rhs.intPtr; }
friend bool operator > (const RuleOfFive& lhs, const int rhs) { return *lhs.intPtr > rhs; }
friend bool operator > (const int lhs, const RuleOfFive& rhs) { return lhs > *rhs.intPtr; }
// >=
friend bool operator >= (const RuleOfFive& lhs, const RuleOfFive& rhs) { return *lhs.intPtr >= *rhs.intPtr; }
friend bool operator >= (const RuleOfFive& lhs, const int rhs) { return *lhs.intPtr >= rhs; }
friend bool operator >= (const int lhs, const RuleOfFive& rhs) { return lhs >= *rhs.intPtr; }
// <
friend bool operator < (const RuleOfFive& lhs, const RuleOfFive& rhs) { return *lhs.intPtr < *rhs.intPtr; }
friend bool operator < (const RuleOfFive& lhs, const int rhs) { return *lhs.intPtr < rhs; }
friend bool operator < (const int lhs, const RuleOfFive& rhs) { return lhs < *rhs.intPtr; }
// <=
friend bool operator <= (const RuleOfFive& lhs, const RuleOfFive& rhs) { return *lhs.intPtr <= *rhs.intPtr; }
friend bool operator <= (const RuleOfFive& lhs, const int rhs) { return *lhs.intPtr <= rhs; }
friend bool operator <= (const int lhs, const RuleOfFive& rhs) { return lhs <= *rhs.intPtr; }
// Compound operators
// +=
RuleOfFive& operator += (const RuleOfFive& rhs)
{
*intPtr += *rhs.intPtr;
return *this;
}
RuleOfFive& operator += (int rhs)
{
*intPtr += rhs;
return *this;
}
// -=
RuleOfFive& operator -= (const RuleOfFive& rhs)
{
*intPtr -= *rhs.intPtr;
return *this;
}
RuleOfFive& operator -= (int rhs)
{
*intPtr -= rhs;
return *this;
}
// *=
RuleOfFive& operator *= (const RuleOfFive& rhs)
{
*intPtr *= *rhs.intPtr;
return *this;
}
RuleOfFive& operator *= (int rhs)
{
*intPtr *= rhs;
return *this;
}
// /=
RuleOfFive& operator /= (const RuleOfFive& rhs)
{
*intPtr /= *rhs.intPtr;
return *this;
}
RuleOfFive& operator /= (int rhs)
{
*intPtr /= rhs;
return *this;
}
// %=
RuleOfFive& operator %= (const RuleOfFive& rhs)
{
*intPtr %= *rhs.intPtr;
return *this;
}
RuleOfFive& operator %= (int rhs)
{
*intPtr %= rhs;
return *this;
}
// Increment/Decrement
RuleOfFive& operator ++ ()
{
++(*intPtr);
return *this;
}
RuleOfFive& operator -- ()
{
--(*intPtr);
return *this;
}
// Output stream
friend std::ostream& operator << (std::ostream& os, const RuleOfFive& input)
{
return os << *input.intPtr;
}
private:
int* intPtr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment