开发者

Can you call the arrow (->) operator function manually?

开发者 https://www.devze.com 2023-03-22 00:26 出处:网络
If I have a Foo *foo, I can say foo->bar().Is it possible to call the operator->() function manually?And if so, how would I pass it bar()?

If I have a Foo *foo, I can say foo->bar(). Is it possible to call the operator->() function manually? And if so, how would I pass it bar()?

Does it make a difference if it is Foo foo instead?

Maybe som开发者_开发知识库ething like foo.operator->(bar)?


Yes, you can. With overloaded -> the foo->bar() expression is interpreted by the compiler as foo.operator->()->bar(). And this is exactly how you can call it "manually": foo.operator->()->bar().

If your overloaded operator -> function is implemented "properly", i.e. it returns something that also supports operator -> then there's not much point in using the "manual" syntax, since it is doing the same thing as the "non-manual" one.

The only case you'd need the "manual" syntax is when your implementation of overloaded operator -> returns something that does not support another application of ->. An int value, for example.


Yes.

(*foo)->bar();               //syntax one   (implicit)
(*foo)->operaror->()->bar(); //syntax two   (explicit)
 foo->operator->()->bar();   //syntax three (explicit)


Is it possible to call the operator->() function manually?

There is no operator->() function because foo in your example is a pointer. For pointers, the behavior of -> is defined by the language.

If the type Foo has an operator->() function, and you have Foo *foo defined, you can do this to call the operator->() function:

(*foo)->...;

Or you can use the direct call syntax:

(*foo)operator->()->...;
0

精彩评论

暂无评论...
验证码 换一张
取 消