How to Call Member Function from Pointer?

Hi, I'm kind of new to C++ programming, so if anyone could help me with this question, I'd really appreciate it.

So I've got a class of object called student, to represent a student. This class has a member function called getGPA() to return that student's GPA. I've got a pointer to student called thisStudent. Let's say it's pointing to a student named Billy. I'm trying to call getGPA() from the pointer, but I'm not exactly sure how. This is what I'm trying:

int main(){

Student Billy(), John(), Harry();

Student * thisStudent = Billy;
float GPA = *thisStudent.getGPA(); //This doesn't work

}

When I try this, I'm told that getGPA() is undeclared.

I know this is wrong, but I'm still sort of struggling with pointers, so if someone could point out to me what I'm doing wrong and the right way to go about this, I'd be very grateful.

Thanks!
You shall write

Student * thisStudent = &Billy;

and then

float GPA = thisStudent->getGPA();

I'd like to append that these declarations

Student Billy(), John(), Harry();

declare functions that return a value of type Student. You shall write

Student Billy, John, Harry;
Last edited on
The line:
float GPA = *thisStudent.getGPA();

doesn't work because of operator precedence. It is interpreted as

float GPA = *(thisStudent.getGPA());

To make it work you would have to write:

float GPA = (*thisStudent).getGPA();

The parentheses around *thisStudent yields the student object itself which then allows you to use the dot notation to access the class member.

The normal way to access class members through a pointer is through the arrow operator as vlad showed above. In fact, (*thisStudent). and thisStudent-> are equivalent.
Perfect! And with that, my assignment works like a charm. Thanks guys!
Topic archived. No new replies allowed.