超载!因子运算符 [英] overloading ! operator for factorial

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

问题描述

大家好,


我想超载!运算符,所以当我在main中写这个
x!

我可以得到x的阶乘。

问题是,运算符我想重载没有参数。

大多数例子我见过,有一个参数。我想知道这是否会导致

问题。

我的代码是


float A :: operator!()


{


浮动产品= 0.0;

if(n!= 0)


{


for(; n> 0; n--)//我们省略初始化表达式


product * = n; //因为我们用构造函数初始化了n

返回产品;


}


else


返回1.0;


}

Hi everyone,

I want to overload the ! operator, so that when I write this in main
x!
I can get the factorial of x.
The problem is, that the operator I want to overload takes no parameter.
Most examples I''ver seen, have a parameter. I wonder if this causes the
problem.
My code is

float A::operator !()

{

float product = 0.0;
if (n != 0)

{

for ( ; n > 0 ; n--) // We omit the initialization expression

product *= n; // since we have initialized n with the constuctor
return product;

}

else

return 1.0;

}

推荐答案




Silver写道:


Silver wrote:
大家好,

我想超载!操作员,所以当我在主要的时候写这个
x!
Hi everyone,

I want to overload the ! operator, so that when I write this in main
x!




这怎么样?运营商!适用于

权利的论据。



How is that going to work? operator! applies to the argument to the
right of it.


Silver写道:

我想超载!操作员,所以当我在主要的时候写这个
x!

Hi everyone,

I want to overload the ! operator, so that when I write this in main
x!




这是不可能的。你能得到的最好的是

!x


但它仍然不是一个好主意。原因是:在C ++中,所有标准的

运算符都具有预定义,在这种情况下!这是''不'。

因此,在阅读源代码时,每个人都看到了这一行:


i =!x;


立即认为:我被分配''不是x'而不是我被分配了

阶乘x.


那里当涉及到运算符重载时,这是一个简单的规则:像int一样。

这意味着:尝试以实现它们的方式实现运算符

for int(如果你可以)。原则上你可以创建一个类,它有一个运算符+ b $ b,它相当于乘法。但是这会让人感到困惑,因为每个人都在阅读a + b时想到的是加法而不是
的乘法。这就是最好的事情:像int一样。


无论如何:


#include< iostream>

使用命名空间std;


class A

{

public:

A (int n):m_n(n){}

double运算符!()const {

double Result = 1.0;

for(int i = 1; i< = m_n; ++ i)

结果* = i;

返回结果;

}

私人:

int m_n;

};


int main()

{

cout<< !A(5)<<结束;


A测试(8);

cout<< !测试<<结束;

}


-

Karl Heinz Buchegger
kb ****** @ gascad.at


我现在开始了解运算符重载的使用。

谢谢。


" Karl Heinz Buchegger" < KB ****** @ gascad.at>在消息中写道

新闻:3F *************** @ gascad.at ...
I now begin to understand the use of operator overloading.
Thanks.

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...
Silver写道:

大家好,

我想超载!操作员,所以当我在主要的时候写这个
x!
这是不可能的。你能得到的最好的将是
!x

但它仍然不是一个好主意。原因是:在C ++中,所有标准的运算符都具有预定义,在这种情况下!这是''不'。
因此,当阅读源代码时,每个人都看到了这一行:

我=!x;

立即认为:我被分配''不是x'而不是我被分配了x的因子。

当涉及到运算符重载时有一个简单的规则:do as int

Hi everyone,

I want to overload the ! operator, so that when I write this in main
x!
That''s not possible. The best you can get would be
!x

But it still is not a good idea. The reason is: In C++ all the standard
operators have a predefined meaning, in the case of ! this is ''not''.
So when reading through a source code, everybody seeing the line:

i = !x;

thinks immediatly: i gets assigned ''not x'' and not i gets assigned the
factorial of x.

There is a simple rule when it comes to operator overloading: Do as int



的确如此。这意味着:尝试以一种方式实现运算符,就像为int实现
它们一样(如果可以的话)。原则上你可以创建一个类,它有一个操作符+,它相当于乘法。但是这会让人感到困惑,因为每个人都在阅读a + b时想到的是加法而不是乘法。这就是它的意思:像int一样。

无论如何:

#include< iostream>
使用命名空间std;

A级
{
公开:
A(int n):m_n(n){}
double运算符!()const {
双结果= 1.0;
for(int i = 1; i< = m_n; ++ i)
结果* = i;
返回结果;
}
私人:
int m_n;
};

int main()
{
cout<< !A(5)<<结束;

A测试(8);
cout<< !测试<< endl;
}
-
Karl Heinz Buchegger
kb * *****@gascad.at



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

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