0

I want to overload array opperator (->) as a standard operator for pointers and additionaly whant to show somethink on the console.

And I did ...

Gamer.h file (Gamer class with nested Collecion class) :

class Gamer {

class Collection
{
public:
    Collection();
    Collection* operator->();
}
public:
Gamer();

Collection *deck;
}

Gamer.cpp

...
Gamer::Collection* Gamer::Collection::operator->()
{
    cout << "Pointer on deck >> " << this << endl;
    return this;
}
...

main.cpp - here i declare and alloc memory for Gamer object who have collection field inside (as pointer)

Gamer *gamer;

Gamer::allocGamer(gamer);

When i want to use overloaded array opperand I can't do :

gamer->deck->();

But I can :

gamer->deck->operator->();

The question is why I cant just call gamer->deck->(); and have to call gamer->deck->operator->(); instead ?

And second question - How I should overload arrow opperand to have direct access to overloaded opperand like gamer->deck->(); ?

1 Answers1

3

operator-> is a binary operator. "binary operator" means it takes two operands. For example:

  deck -> foo
// ^        ^---- second operand
// |
// \------- first operand

It is not possible to write deck-> without a second operand.

Calling it as deck->operator->(); is a bit of a hack, which is legal but you should not design your code to rely on that because it will be confusing to readers.

The purpose of overloading operator-> is so that you can write deck->foo. If you do not want to write deck->foo then you should not use operator-> for whatever you are trying to do.

For example, if you just want a function to retrieve foo then call it get_foo() , not operator->().

Note: Gamer::allocGamer(gamer); is suspicious (if allocGamer takes its parameter by value it's a bug, and if it takes it by reference then this is an unusual idiom that will be confusing to people reading your code). Your code will be simpler if you don't use memory management, and don't force users of your class to use manual memory management either.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • I know that i can make a getter but I have to do it throw arrow opperator. It is even possible ? –  Dec 19 '16 at 22:32
  • @PatrykJanik sorry I'm not sure what you are asking there – M.M Dec 19 '16 at 22:36
  • I cant edit my post. Instead of throw should be by. I dont know the way how I should transform gamer->deck->operator->(); for gamer->deck->(); to call the opperand overloaded function. –  Dec 19 '16 at 22:41
  • So that's means when i should call for example gamer->deck->showCardCollection(); method first will be launched overloaded opperator function then showCardCollection() ? If yes I putted breakpoint into Gamer::Collection::operator->() and it even not entered into that function. –  Dec 19 '16 at 23:11
  • @PatrykJanik your code in the question is not a good example , the overloaded `operator->` will never be invoked for `deck->`. because `deck` is a pointer. Like all overloaded operators (and indeed all member functions), It is invoked only when the left-hand side is an object of `Gamer` (not a pointer). Using `->` on a pointer uses the built-in `->`, not an overloaded operator. For example `(*gamer->deck)->showCardCollection()` would invoke the operator. – M.M Dec 19 '16 at 23:17
  • I think you are confusing yourself by using this complicated design; to progress with your code it might be better if you start a new question explaining what you are trying to achieve at a higher level (and the solution will almost certainly not involve `operator->`). – M.M Dec 19 '16 at 23:17