tags:

views:

227

answers:

3

Suppose I have:

Foo foo;

is there a shorthand for this?

foo.operator->().operator()(1, 2);
+4  A: 

Well... Yes. The shorter form would look as

foo.operator->()(1, 2)

As for eliminating the operator -> part... From the information you supplied so far it is impossible to say, but if it is implemented the way I can guess it is implemented (judging from your expression), then you can't eliminate it.

In C++ the use of overloaded -> operator in an expression is interpreted as a chain of repetitive overloaded -> calls, which eventually ends in a built-in -> invocation. This means that at some point the overloaded -> must return a pointer. Your overloaded -> obviously doesn't return a pointer. So, in order to use it you have no other choice but to spell it out explicitly as operator ->().

AndreyT
+1 for correctly specifying exactly what's needed to use `operator->`. I didn't realize that a compliant program could use non-pointer `operator->`, but it's possible!
Potatoswatter
+3  A: 

Assuming you actually meant foo.operator->().operator()(1, 2), and that you have control over the class Foo, a simpler form would be (*foo)(1, 2). It requires the operator* to that defined though, but since we usually expect foo->bar to be equivalent to (*foo).bar, it seems reasonable.

If your Foo is a smart pointer class of some sort, which points to an object which defines an operator(), this would be the most concise way of calling the object's operator().

But without more detail (and without you providing an expression that's actually valid C++ -- there's no way in which operator(1, 2) as you wrote it can be valid), it's impossible to answer your question. I'm just guessing at what you're trying to do.

jalf
A: 

Well, no, but, assuming you have write permissions to the class, you could define another member function that calls operator(), and then you'd have something like:

foo->myfunc(1,2);

That you find yourself in this position is a sign that you (or the person who wrote this class) is being a bit too cute with operator overloading.

JohnMcG
Err... No. It won't work. The OP's overloaded `->` returns a non-pointer. This immediately means that any attempts to call it as `foo->...` are ill-formed. It simply won't compile. (There's a chance for it to work if the overloaded `->` returns something that in turn has its own overloaded `->`, but it would be a stretch to make such a guess without any extra info.)
AndreyT
I see that -- f.operator->().myFunc(1,2) works.Which I guess underscores the point that this seems to be a poor use of operator overloading.
JohnMcG