如何重用朋友运营商? [英] How to reuse a friend operator?

查看:67
本文介绍了如何重用朋友运营商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



您好,


是否可以重复使用在类中定义的朋友运算符?
?我想获得以下行为:


类整数{


[...]

整数运算符+(signed long int v)const {


//它以某种方式完成它的工作

}


朋友内联整数运算符+(签名long int lhs,const integer&

rhs){


返回rhs.operator +(lhs ); //这是允许的,因为

交换性

}


朋友内联整数运算符+(signed int lhs,const integer& rhs){


返回运算符+(static_cast< signed long int>(lhs),rhs); //这里是

问题

}

};

整数i;

i + 10; //使用第一个运算符

10L + i; //使用第二个运算符

10 + i; //使用第三个然后将控制传递给第二个

问题是编译器(G ++ 3.4)没有看到运算符

声明为朋友。它会显示一条消息,表明没有运营商可以匹配

并显示备选列表,但只列出本地运营商(

这个例子中只有一个,在实际项目中有6)。当

朋友运营商以标准方式宣布时,即在班级之外,

一切正常。所以问题是:如何在第三个运营商的机构中限定运营商+()和/ b
来使用第二个运营商?这些

ad hoc方法都不起作用:


" return :: operator +("

" return integer) :: operator +("

" return friend operator +("


)Best to

Piotr Wyderski


Hello,

is it possible to reuse a friend operator which is defined
inside a class? I''d like to obtain the following behaviour:

class integer {

[...]

integer operator +(signed long int v) const {

// It somehow does its job
}

friend inline integer operator +(signed long int lhs, const integer&
rhs) {

return rhs.operator +(lhs); // This is allowed because of
commutativity
}

friend inline integer operator +(signed int lhs, const integer& rhs) {

return operator +(static_cast<signed long int>(lhs),rhs); // Here''s
the problem
}
};
integer i;
i + 10; // the first operator is used
10L + i; // the second operator is used
10 + i; // the third is used and then control is passed to the second one
The problem is that the compiler (G++ 3.4) doesn''t see the operators
declared as friends. It displays a message that no operator can be matched
and shows the list of alternatives, but only local operators are listed (in
this example there''s only one, in the real project there are 6). When the
friend operators are declared in a standard way, i.e. outside of the class,
everything works OK. So the question is: how to qualify the "operator +()"
in the third operator''s body to use the second one? None of these
ad hoc approaches works:

"return ::operator +("
"return integer::operator +("
"return friend operator +("

Best regards
Piotr Wyderski

推荐答案

2004年7月22日星期四17:22:12 -0700,Piotr Wyderski写道:
On Thu, 22 Jul 2004 17:22:12 -0700, Piotr Wyderski wrote:
你好,

是否可以重用在
类中定义的友元操作符?我想获得以下行为:
类整数{

[...]
整数运算符+(带符号long int v)const {

//它以某种方式完成它工作

朋友内联整数运算符+(签名long int lhs,const
整数&
rhs){

返回rhs。 operator +(lhs); //这是允许的,因为
交换
}

朋友内联teger operator +(signed int lhs,const integer&
rhs){

返回运算符+(static_cast< signed long int>(lhs),rhs); //
这里是
问题
}
};

整数i;
i + 10; //第一个运算符使用10L + i; //第二个运算符是使用10 + i; //使用第三个然后控制传递给
第二个
Hello,

is it possible to reuse a friend operator which is defined inside a
class? I''d like to obtain the following behaviour:

class integer {

[...]

integer operator +(signed long int v) const {

// It somehow does its job
}

friend inline integer operator +(signed long int lhs, const
integer&
rhs) {

return rhs.operator +(lhs); // This is allowed because of
commutativity
}

friend inline integer operator +(signed int lhs, const integer&
rhs) {

return operator +(static_cast<signed long int>(lhs),rhs); //
Here''s
the problem
}
};
integer i;
i + 10; // the first operator is used 10L + i; // the second operator is
used 10 + i; // the third is used and then control is passed to the
second one




如果您要解决的问题是向一个整数添加一个整数

类左侧或右侧,以下是

传统的做法:


#include< iostream>


使用命名空间std;


class Integer

{

long value_;


朋友ostream&运营商LT;< (ostream&,Integer const&);


public:


整数(长值= 0)



value_(价值)

{}


整数& operator + =(整数const& rhs)

{

value_ + = rhs.value_;

return * this;

}

};


整数运算符+(整数const& lhs,整数const& rhs)

{

整数和= lhs;

总和+ = rhs;

返还金额;

}


ostream&运营商LT;< (ostream& os,Integer const& i)

{

return os<< i.value_;

}


int main()

{

整数i(42 );

cout<< (i + 10)<< ''\ n''

<< (10L + i)<< ''\ n''

<< (10 + i)<< ''\ n''

<< (i +整数(10))<< ''\ n'';

}


阿里



If the problem that you are trying to solve is adding an integer to a
class either on the left or on the right hand side, the following is
the traditional way of doing that:

#include <iostream>

using namespace std;

class Integer
{
long value_;

friend ostream & operator<< (ostream &, Integer const &);

public:

Integer(long value = 0)
:
value_(value)
{}

Integer & operator+= (Integer const & rhs)
{
value_ += rhs.value_;
return *this;
}
};

Integer operator+ (Integer const & lhs, Integer const & rhs)
{
Integer sum = lhs;
sum += rhs;
return sum;
}

ostream & operator<< (ostream & os, Integer const & i)
{
return os << i.value_;
}

int main()
{
Integer i(42);
cout << (i + 10) << ''\n''
<< (10L + i) << ''\n''
<< (10 + i) << ''\n''
<< (i + Integer(10)) << ''\n'';
}

Ali




Ali Cehreli写道:

Ali Cehreli wrote:
如果您要解决的问题是在左侧或右侧为
类添加一个整数,以下是传统的做法:
If the problem that you are trying to solve is adding an integer to a
class either on the left or on the right hand side, the following is
the traditional way of doing that:




不,问题不在于如何在类中添加整数。

这种方式在这种特殊情况下非常低效,因为它需要

a临时对象,这在这里相当大。我有一个非常有效的

这类运算符的实现,我想使用它。问题

是当朋友运营商被定义在整数内部时。 class,

编译器不允许使用另一个朋友操作符来实现当前的
。当它们在课外定义时,它就可以工作。

我的问题是:为什么编译器的行为如此,以及如何在不移动运算符的情况下解决这个问题外面?

没人知道?我是否在C ++中找到了一个漏洞?



No, the problem is not the question how to add an integer to a class.
This way is very inefficient in this particular case, because it requires
a temporary object, which is quite big here. I have a very efficient
implementation of such operators and I want to use it. The problem
is that when the friend operators are defined inside the "integer" class,
the compiler doesn''t allow to use another friend operator to implement
the current one. When they are defined outside the class, it works.
My question is: why does the compiler behave like that and how to
get rid of this problem without moving the operators outside?
Nobody knows? Have I found a hole in C++?




Ali Cehreli写道:


没关系,我刚刚找到了解决方案,所以EOT。


Ali Cehreli wrote:

Never mind, I have just found a solution, so EOT.


这篇关于如何重用朋友运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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