2个问题 - 虚拟运算符和运算符= [英] 2 questions--virtual operators and operator=
问题描述
1 - 操作员可以是虚拟的吗?
2 - 操作员=返回参考值与值之间有什么区别?
1--Can operators be virtual?
2--What is the difference between an operator= returning a refernce Vs a value?
推荐答案
" c ++ novice" <是************** @ yahoo.com>在留言中写道
news:34 ************************** @ posting.google.c om ...
"c++novice" <be**************@yahoo.com> wrote in message
news:34**************************@posting.google.c om...
1 - 运营商可以虚拟吗?
是的。
2 - 运营商之间的区别=返回推荐值与
1--Can operators be virtual?
Yes.
2--What is the difference between an operator= returning a refernce Vs a
值?
与使用任何
其他函数返回引用或值之间的差异相同。
john
value?
Same as the difference between returning a reference or a value with any
other function.
john
" c ++ novice" <是************** @ yahoo.com>在留言中写道
news:34 ************************** @ posting.google.c om ...
"c++novice" <be**************@yahoo.com> wrote in message
news:34**************************@posting.google.c om...
1 - 运营商可以虚拟吗?
是的。
2 - 运营商之间的区别=返回推荐值与
1--Can operators be virtual?
Yes.
2--What is the difference between an operator= returning a refernce Vs a
值?
与使用任何
其他函数返回引用或值之间的差异相同。
john
value?
Same as the difference between returning a reference or a value with any
other function.
john
" John Harrison" <乔************* @ hotmail.com>在消息中写道
news:c4 ************* @ ID-196037.news.uni-berlin.de ...
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c4*************@ID-196037.news.uni-berlin.de...
c ++新手 <是************** @ yahoo.com>在消息中写道
新闻:34 ************************** @ posting.google.c om ...
"c++novice" <be**************@yahoo.com> wrote in message
news:34**************************@posting.google.c om...
1 - 运营商可以虚拟吗?
1--Can operators be virtual?
是的。
Yes.
2 - 运营商=返回有什么区别引用是否
2--What is the difference between an operator= returning a refernce Vs a
值?
与使用任何其他函数返回引用或值之间的区别相同。
value?
Same as the difference between returning a reference or a value with any
other function.
也许这不是最有帮助的答案。这是一个简短的程序,运行它
并查看结果,然后将operator = return类型更改为一个值并再次运行
,看看它有什么不同。
#include< iostream>
struct X
{
X&安培; operator =(const X& rhs)
{
x = rhs.x;
返回* this;
}
int x;
};
int main()
{
X a,b,c;
ax = 1;
bx = 2;
cx = 3;
(a = b)= c;
std :: cout<< a.x<< ''\\ n';;
}
但这需要对C ++有一个更深入的了解。所有你真正需要
知道返回参考是正确的事情。
john
Maybe that wasn''t the most helpful answer. Here''s a short program, run it
and see the result, then change the operator= return type to a value and run
it again, see what difference it makes.
#include <iostream>
struct X
{
X& operator=(const X& rhs)
{
x = rhs.x;
return *this;
}
int x;
};
int main()
{
X a, b, c;
a.x = 1;
b.x = 2;
c.x = 3;
(a = b) = c;
std::cout << a.x << ''\n'';
}
But this needs quite an advanced understanding of C++. All you really need
to know is returning a reference is the right thing to do.
john
这篇关于2个问题 - 虚拟运算符和运算符=的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!