运算符重载为主要类型 [英] operator overloading for primative types

查看:95
本文介绍了运算符重载为主要类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




似乎C ++不允许为主要的

类型重载运算符,例如int,double。我想知道它是否真的有没有

一些走动?


谢谢,




#include< iostream>

#include< complex>


命名空间我的

{

int operator /(int x,int y){

if(x> 0)

返回x / y;

其他

返回x / y - 1;

}


void test(int x, int y){

std :: cout<< x<< " /" << y<< " " << x / y<< std :: endl;

}


}


int main(){

my :: test(10,3);

my :: test(-10,3);

my :: test(10,-3);

my :: test(-10,-3);

}

解决方案

Pe*******@gmail.com 写道:





似乎C ++不允许为主要的

类型重载运算符,例如int,double。我想知道它是否是



这是真的。参见条款[13.3.1.2/1]

如果表达式中的运算符的操作数没有类型为

或枚举,则假定运算符为是一个内置的操作员和

根据第5条解释。


或有一些绕行?



我不知道透明的任何工作。您必须装备您的操作数

,以便其中至少有一个不是内置类型。

最佳


Kai-Uwe Bux


Pe ******* @ gmail .com 写道:





似乎C ++不允许重载运算符为主要

类型,例如int,double。我想知道它是否真的有没有

一些走动?



幸运的是你不能这样做会产生不会像其他人期望的那样行事的代码,这是一个难以维持的噩梦。


如果你想要一个有特殊行为的类型,用适当的操作符将它包装在一个类中。


-

Ian Collins。


9月26日晚上10:51,Ian Collins< ian-n ... @ hotmail.comwrote:


PengYu。 .. @ gmail.com写道:




似乎C ++不允许重载运算符为主要的

类型,例如int,double。我想知道它是否真的还是有一些走动?



幸运的是,你不能这样做会产生一些代码,这些代码不会像其他人所希望的那样表现出来。噩梦要维持。



我同意。有些人提倡类似的东西应该通过引入std :: intrusive :: list类型来完成

。我认为最好让它变得更好 -
http ://tinyurl.com/2q5p34

Brian Wood

Ebenezer Enterprises
www.webebenezer.net


Hi,

It seems that C++ does not allow overloading operators for primative
types, e.g. int, double. I''m wondering whether it is ture or there is
some walk-around?

Thanks,
Peng

#include <iostream>
#include <complex>

namespace my
{
int operator/(int x, int y) {
if(x>0)
return x/y;
else
return x/y - 1;
}

void test(int x, int y) {
std::cout << x << "/" << y << " " << x/y << std::endl;
}

}

int main() {
my::test(10, 3);
my::test(-10, 3);
my::test(10, -3);
my::test(-10, -3);
}

解决方案

Pe*******@gmail.com wrote:

Hi,

It seems that C++ does not allow overloading operators for primative
types, e.g. int, double. I''m wondering whether it is ture

It is true. See clause [13.3.1.2/1]
If no operand of an operator in an expression has a type that is a class
or an enumeration, the operator is assumed to be a built-in operator and
interpreted according to clause 5.

or there is some walk-around?

I don''t know of any that work transparently. You have to rig your operands
so that at least one of them is not of built-in type.
Best

Kai-Uwe Bux


Pe*******@gmail.com wrote:

Hi,

It seems that C++ does not allow overloading operators for primative
types, e.g. int, double. I''m wondering whether it is ture or there is
some walk-around?

Luckily you can''t, doing so would produce code that doesn''t behave as
others would expect it to, a nightmare to maintain.

If you want a type with specialised behavior, wrap it in a class with
the appropriate operators.

--
Ian Collins.


On Sep 26, 10:51 pm, Ian Collins <ian-n...@hotmail.comwrote:

PengYu...@gmail.com wrote:

Hi,

It seems that C++ does not allow overloading operators for primative
types, e.g. int, double. I''m wondering whether it is ture or there is
some walk-around?


Luckily you can''t, doing so would produce code that doesn''t behave as
others would expect it to, a nightmare to maintain.

I agree. Some people are advocating something similar should be done
by introducing a std::intrusive::list type. I think it would be
better to make it ilist -
http://tinyurl.com/2q5p34
Brian Wood
Ebenezer Enterprises
www.webebenezer.net


这篇关于运算符重载为主要类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆