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:
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.
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.