怀疑 - C ++ FAQ Lite - Item [14.5] [英] doubt on - C++ FAQ Lite - Item [14.5]

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

问题描述



http://www.parashift.com/c++-faq-lit....html#faq-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

推荐答案

su ** ************@yahoo.com 写道:



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


以下提到:


(成员函数不允许提升左手参数,

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

的成员函数调用)


我不明白该部分因为那会改变
$ b $的等级b对象是成员函数调用的接收者


请举例说明。
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.



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


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


12月4日14:56,subramanian10 ... @ yahoo.com,India

< subramanian10 ... @ yahoo.comwrote:
On 4 Dec., 14:56, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:



http://www.parashift.com/c++-faq-lit....html# faq-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



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


有时朋友在语法上更好(例如,在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


* 12月4日晚上7:20, Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
* On Dec 4, 7:20 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

subramanian10 ... @ yahoo.com写道:
subramanian10...@yahoo.com wrote:


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.



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


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]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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