T
T
Therapyx2016-01-14 15:02:12
OOP
Therapyx, 2016-01-14 15:02:12

Difference between this ->, (*this), className::, and "nothing" before attributes in c++ OOP?

Let's say I have a class named Student. The header file contains the declaration of the vecStudents vector. The cpp file contains functions of this kind. In the function below, I have provided 4 different examples.
Duck, the professor was embarrassed that I wrote Student::vecStudents.size(); or just vecStudents.size(); after which he gave 2 more other examples, but he could not explain to me the differences in each of the options (not to mention the "detailed explanation") :) Therefore, in desperation, I turn here with such a question ...

void Student::functionName(param) {
int size = (*this).vecStudents.size(); // или
int size = this -> vecStudents.size(); // или
int size = Student::vecStudents.size(); // или
int size = vecStudents.size(); // или
.... дальшейний код...
}

Of course, I would like to read in more detail, in what cases and why it is necessary or necessary to use one of the options))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2016-01-14
@Therapyx

When you use this-> you are using it as a pointer/reference type.
(*this) is a pointer dereference, i.e. cast to value type.
And already access to members of type will be through a point.
Student::functionName() is for calling static methods of a class i.e. without an instance/class object

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question