运算符重载
Posted roadmap
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了运算符重载相关的知识,希望对你有一定的参考价值。
#include <stdio.h>
#include "stdafx.h"
struct BigNumber {
private:
unsigned int low;
unsigned int high;
public:
BigNumber(int x, int y)
{
this->low = x;
this->high = y;
}
BigNumber operator ++();
void operator =(const BigNumber &n);
bool operator >(const BigNumber &n);
bool operator ==(const BigNumber &n);
void print();
};
BigNumber BigNumber::operator ++()
{
if(this->low == 0xFFFFFFFF) {
this->low = 0;
this->high++;
} else
this->low++;
return *this;
}
void BigNumber::operator =(const BigNumber &n)
{
this->low = n.low;
this->high = n.high;
}
bool BigNumber::operator >(const BigNumber &n)
{
if (this->high > n.high)
return true;
else if (this->high < n.high)
return false;
else if (this->low > n.low)
return true;
else if (this->low <= n.low)
return false;
}
bool BigNumber::operator ==(const BigNumber &n)
{
if (this->high == n.high && this->low == n.low)
return true;
else
return false;
}
void BigNumber::print()
{
printf("low: %u, high: %u\n", this->low, this->high);
}
int main(int argc, char* argv[])
{
//BigNumber n(1, 2);
BigNumber n(0xFFFFFFFF, 2);
n.print();
n++;
n.print();
BigNumber n1(1, 2);
BigNumber n2 = n1;
n2.print();
printf("n1 %s n2\n", n1 == n2 ? "==" : "<");
n2++;
printf("n1 %c n2\n", n1 > n2 ? ‘>‘ : ‘<‘);
getchar();
return 0;
}
以上是关于运算符重载的主要内容,如果未能解决你的问题,请参考以下文章