why do we use these ->

Im working with some frame work for a university project and :

player1->passCardToPile(*pile, 1, true); //this works it passes a card from player1's hand to the pile which is just a pile of cards.

but

player1.passCardToPile(*pile, 1, true);

doesn't work , is it because player1 is a pointer. also passcardtopile is in a class type.

Just a quick question thanks! :)
. is for actual objects themselves and references.
-> is for pointers and objects that act like pointers.
okay thanks :)
I forgot to mention, these two lines are the same:
1
2
a->b;
(*a).b;
If you would like to access a function in a class and you created a pointer object then you use ->.

every function in a class has at least implicit parameter which is ->. This is to let the compiler distinguish between the functions of the objects.
a->b;
(*a).b;

They are same. The first one makes perfect sense in terms of naming because it suggets its purpose. In other words, it points to the objet..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

class Circle {
  public:
    void printValue();
};

main()
{
  Circle x;
  Circle *ptr = &x;

  x.printValue();
  ptr->printValue();
}

void Circle::printValue(){
    cout << "\n Hello World!" << endl;
}


As you see there are two ways to access to printValue() function. If you didn't get it let me know to elaborate.
Last edited on
Topic archived. No new replies allowed.