Answer the question
In order to leave comments, you need to log in
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(); // или
.... дальшейний код...
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question