假设有如下的代码:
T a,b,c; //T为一个class
c = a + b;T的“+”号重载函数在很多文章上似乎有两种写法:
写法一:
T T::operator+(T& a, T& b)
{
T c;
c.m = a.m + b.m;
c.n = a.n + b.n;
return c;
}
写法二:
T T::operator+(T& a)
{
T c;
c.m = this->m + a.m;
c.n = this->n + a.n;
return c;
}不知那一种是正确的?
T a,b,c; //T为一个class
c = a + b;T的“+”号重载函数在很多文章上似乎有两种写法:
写法一:
T T::operator+(T& a, T& b)
{
T c;
c.m = a.m + b.m;
c.n = a.n + b.n;
return c;
}
写法二:
T T::operator+(T& a)
{
T c;
c.m = this->m + a.m;
c.n = this->n + a.n;
return c;
}不知那一种是正确的?
解决方案 »
- 【求助】请教几个关于WinCE内存物理地址的的问题
- 编译的时候说找不到TEXT这个类型,为什么
- 怎么在另一函数退出后再同步退出
- VC里怎么使用带参数的update语句,偶没分了但是很着急!
- 菜鸟提问,请教高手?
- 用ShFormatDriver格式化时,要隐藏显示与格式化有关的窗体,怎样实现?
- 请教啦:刚接触网络编程,有些函数名称觉得挺奇怪的,比如:WSAStartup和WSACleanup,它们为什么要在前面加WSA?
- 3d效果的正方形!!
- 一个很弱的问题,请大家多给指点。
- 在动态库中导出一个变量怎么实现
- 急!如何正确显示数据表中的时间数据?
- 请问如何处理ShellExecute中的起始位置参数
2.right1.//T T::operator+(T& a, T& b) // it is not member function of class T
T operator+(T& a, T& b)
{
T c;
c.m = a.m + b.m;
c.n = a.n + b.n;
return c;
}
if you define the operator like this:
T T::operator+(T& a, T& b), then operator+ take 3 paremater, so has too many parameters
if you want to define operator+ in class T,it must like T T::operator+(T& a)
else it must like T operator+(T& a, T& b)
friend T operator+(T& a, T& b);T operator+(T& a, T& b) // global function!
{
T c;
c.m = a.m + b.m;
c.n = a.n + b.n;
return c;
}
D:\VCѧϰ\Caculate\CaculateUnit.cpp(25) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
D:\VCѧϰ\Caculate\CaculateUnit.cpp(75) : error C2143: syntax error : missing ';' before '+'
D:\VCѧϰ\Caculate\CaculateUnit.cpp(75) : error C2501: 'T' : missing storage-class or type specifiers
D:\VCѧϰ\Caculate\CaculateUnit.cpp(75) : fatal error C1004: unexpected end of file found