本文介绍了怀疑 - C ++ FAQ Lite - Item [14.5]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述






提到以下内容:


(成员函数不允许推广左手参数,

因为这会改变作为收件人的对象的类

成员函数调用)


我不明白该部分因为这会改变作为收件人的

对象的类成员函数调用


请举例说明。


谢谢

V.Subramanian

In

http://www.parashift.com/c++-faq-lit....html#faq-14.5

the following is mentioned:

(member functions don''t allow promotion of the left hand argument,
since that would change the class of the object that is the recipient
of the member function invocation)

I do not understand the part "since that would change the class of the
object that is the recipient of the member function invocation"

Kindly explain with an example.

Thanks
V.Subramanian

推荐答案



你读的哪本书没有解释操作员超载?


struct A {

A(int){}

运算符+(A const&)const {return A(0); }

};


int main(){

A a(42);


a + 77; //需要从''a'调用A :: operator +的数字

//将''77''提升为另一个''A''(右操作数)


33 + a; //将''33''推广到A?保留''33''原样?

//在哪里寻找''operator +''?

}


V

-

请在通过电子邮件回复时删除资金''A'

我不回复热门帖子回复,请不要问

What book are you reading that doesn''t explain operator overloading?

struct A {
A(int) {}
A operator+(A const&) const { return A(0); }
};

int main() {
A a(42);

a + 77; // Figures that it needs to call A::operator+ from ''a''
// Promotes ''77'' to another ''A'' (the right operand)

33 + a; // promote ''33'' to A? Leave ''33'' as is?
// Where to look for ''operator+''?
}

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask




我相信我应该引用更多的常见问题解答,因为这会让你的问题变得清晰:


有时朋友在语法上更好(例如,在Fred类中,

朋友函数允许Fred参数为秒,而成员

要求它是第一个)。朋友函数的另一个好用是

二进制中缀算术运算符。例如,如果你想允许aFloat +

aComplex,aComplex + aComplex应该是定义为朋友而不是成员的
(成员函数不允许升级)左边的

hand参数,因为这会改变对象的类,即成员函数调用的接收者




它只是意味着使用二进制中缀运算符,如+,*,/或

- ,如果将它们声明为成员函数,则无法使用
$ b调用它们$ b不包括班级成员:


复杂aComplex;

浮动aFloat;

....

aFloat + aComplex现在无法编译,因为这需要

促销aFloat变量,并且不允许

原因所述。您必须将表达式重写为

complex(aFloat)+ aComplex。

将操作符作为朋友将允许上面的表达式

没有明确的转换。


/ Peter

I believe I should quote some more of the FAQ as it makes your
question clearer:

"Sometimes friends are syntactically better (e.g., in class Fred,
friend functions allow the Fred parameter to be second, while members
require it to be first). Another good use of friend functions are the
binary infix arithmetic operators. E.g., aComplex + aComplex should be
defined as a friend rather than a member if you want to allow aFloat +
aComplex as well (member functions don''t allow promotion of the left
hand argument, since that would change the class of the object that is
the recipient of the member function invocation)."

It simply means that with a binary infix operator such as +, *, / or
-, if you declare them as memberfunctions they cant be called with
anything than class members:

complex aComplex;
float aFloat;
....
aFloat + aComplex will now not compile because that would require
promotion of the aFloat variable, and that is not allowed for the
reasons stated. You would have to rewrite the expression as
complex(aFloat) + aComplex.
Having the operator as a friend will allow the expression above
without the explicit conversion.

/Peter




你读的哪本书没有解释操作员超载?


struct A {

A(int){}

运算符+(A const&)const {return A(0); }


};


int main(){

A a(42);


a + 77; //需要从''a'调用A :: operator +的数字

//将''77''提升为另一个''A''(右操作数)


33 + a; //将''33''推广到A?保留''33''原样?

//在哪里寻找''operator +''?


}


V

-


What book are you reading that doesn''t explain operator overloading?

struct A {
A(int) {}
A operator+(A const&) const { return A(0); }

};

int main() {
A a(42);

a + 77; // Figures that it needs to call A::operator+ from ''a''
// Promotes ''77'' to another ''A'' (the right operand)

33 + a; // promote ''33'' to A? Leave ''33'' as is?
// Where to look for ''operator+''?

}

V
--



在表达式33 + a中,因为'a'的类型被称为

A,为什么不能将33转换为A类临时对象并相应评估

表达式?


请澄清。


谢谢

V.Subramanian

In the expression 33 + a, since the type of ''a'' is known as
A, why can''t 33 be converted to a temporary object of type A and the
expression evaluated accordingly ?

Kindly clarify.

Thanks
V.Subramanian


这篇关于怀疑 - C ++ FAQ Lite - Item [14.5]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 21:05