过载“运营商 - >” [英] overload "operator->"
问题描述
我读到有关运营商重载的问题,并且有关于
" operator->()"的问题。如果我有两个这样的课程:
struct A {
void f();
};
struct B {
A * p;
A * operator->(){return p;}
};
然后我们可以这样做:
B b;
b-> f( );
和A''sf()被调用。
然而,超载 - 似乎说它是一元的运算符,并且
是前缀的(因为())。因此,对我来说,看起来我们
应该使用这样的运算符:
B :: operator-> b,
将使用B中的指针成员。然后访问f,我认为我们需要这样做:
(B :: operator-> b) - > f(),
这与b-> f()非常不同。有人可以告诉我为什么
使用b-> f()给我a的f()?
谢谢,
Jess
Hi,
I read about operator overloading and have a question regarding
"operator->()". If I have two classes like this:
struct A{
void f();
};
struct B{
A* p;
A* operator->(){return p;}
};
Then we can do:
B b;
b->f();
and A''s f() is called.
However, the overloading of -seems to say it''s a unary operator, and
is prefixed (because of the ()). Therefore, to me, it looks like we
should use this operator like:
B::operator->b,
which will give use a the pointer member in B. Then to access "f", I
think we need to do:
(B::operator->b)->f(),
which is very different to b->f(). Can someone please tell me why
using b->f() gives me the f() of A?
Thanks,
Jess
推荐答案
Jess写道:
Jess wrote:
我读到有关运算符重载的问题,并且有关于
" operator->()"的问题。如果我有两个这样的课程:
struct A {
void f();
};
struct B {
A * p;
A * operator->(){return p;}
};
然后我们可以这样做:
B b;
b-> f( );
和A''sf()被调用。
然而,超载 - 似乎说它是一元的运算符,并且
是前缀的(因为())。因此,对我而言,看起来我们
应该使用此运算符,如:
B :: operator-> b,
Hi,
I read about operator overloading and have a question regarding
"operator->()". If I have two classes like this:
struct A{
void f();
};
struct B{
A* p;
A* operator->(){return p;}
};
Then we can do:
B b;
b->f();
and A''s f() is called.
However, the overloading of -seems to say it''s a unary operator, and
is prefixed (because of the ()). Therefore, to me, it looks like we
should use this operator like:
B::operator->b,
不,它是b.operator->()的缩写。
No, it''s shorthand for b.operator->().
将使用B中的指针成员。然后要访问f,我认为我们需要这样做:
(B :: operator-> b) - > f( ),
which will give use a the pointer member in B. Then to access "f", I
think we need to do:
(B::operator->b)->f(),
b.operator->() - > f()
-
Ian Collins。
b.operator->()->f()
--
Ian Collins.
5月4日下午1点37分,Ian Collins< ian-n ... @ hotmail.comwrote:
On May 4, 1:37 pm, Ian Collins <ian-n...@hotmail.comwrote:
不,它是b.operator->()的简写。
b.operator->() - > f( )
No, it''s shorthand for b.operator->().
b.operator->()->f()
但声明A * operator->()显示 - >应该用作b的
前缀,不是吗?使用它作为前缀应该给我们 - > b......
此外,你的扩展似乎说我们可以扩展b。
" b.operator->()",我真的看不出它来自哪里....
Jess
But the declaration A* operator->() shows "->" should be used as a
prefix of "b", isn''t it? Using it as a prefix should give us "->b"...
Moreover, your expansion above seems to say we can expand "b" to
"b.operator->()", I really can''t see where it''s from....
Jess
Jess写道:
Jess wrote:
5月4日下午1点37分,Ian Collins< ian-n ... @ hotmail.comwrote:
On May 4, 1:37 pm, Ian Collins <ian-n...@hotmail.comwrote:
>不,它是b.operator->()的缩写。
b.operator->() - > f()
>No, it''s shorthand for b.operator->().
b.operator->()->f()
但声明A * operator->()显示 ; - >"应该用作b的
前缀,不是吗?使用它作为前缀应该给我们 - > b......
But the declaration A* operator->() shows "->" should be used as a
prefix of "b", isn''t it? Using it as a prefix should give us "->b"...
如何?操作员 - 是一元*后缀*操作员。
参见Stroustrup第11.10节。
-
伊恩柯林斯。
How so? Operator -is a unary *postfix* operator.
See section 11.10 of Stroustrup.
--
Ian Collins.
这篇关于过载“运营商 - >”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!