In this tutorial we look at enough surfactant pointer this, to novice programmers get acquainted with it and have an idea of where it is and how it works. Want, to start, consider a few simple examples.
In the first to be determined functions: one of which will write data to the variables, and the second – display them on the screen. In a second example we define class, which will contain two methods, performing the same work, as a function of the first example.
When you consider these examples, note, as a function of the first example and the second class of methods of Example, will take parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <iostream> using namespace std; void enterData(char* someName, int & someAge) { cout << "Ваше имя: "; cin.getline(someName, 128); cout << "Ваш возраст: "; cin >> someAge; } void showData(char* someName, int & someAge) { cout << "\n" << someName << " " << someAge<< endl << endl; } int main() { setlocale(LC_ALL, "rus"); char name[128] = ""; int age = 0; enterData(name, age); showData(name, age); return 0; } |
In this example, you, most likely, we did not see anything new or difficult for you. It's simple – functions only accept settings and perform certain actions with these settings. Now let's write a simple class, which we implement methods very similar to the function of the first example. But something they will vary. Namely, the, that they don't have to take as parameters, class members, to make changes to them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <iostream> using namespace std; class SetShowData { char someName[128]; int someAge; public: void enterData() { cout << "Ваше имя: "; cin.getline(someName, 128); cout << "Ваш возраст: "; cin >> someAge; } void showData() { cout << "\n" << someName << " " << someAge << endl << endl; } }; int main() { setlocale(LC_ALL, "rus"); SetShowData objectOfClass; objectOfClass.enterData(); // вызов метода для ввода данных objectOfClass.showData(); //вызов метода для отображения данных return 0; } |
Consider this example, you saw, that defining methods in the class body, We do not set the parameters in the signature. And, these methods of causing the main function, we also do not specify what data they work. But in some way data is entered precisely those members of the class, identified in body methods.
How methods “understand”, what data and which object class they need to work? The thing is, that in class methods, implicitly passed as a parameter a pointer this (a pointer to the class object). This happens automatically. We do not see it, because this pointer – there is a hidden first parameter of any class method.
Pointer this stores the address of a particular class of object. In this example, it stores the address of the objectobjectOfClass. Thus, he implicitly indicates class methods with the data of the object to work.
I note, that developers still have the opportunity to use the pointer this clearly. If we define a method enterData() with the explicit use of this, it would look like this:
Or so:
Specifically, in these cases, you can do without the explicit use this. But sometimes explicit use can not be avoided. For example, in the following example, this the compiler will not allow to get confused in the names of class members and that accepts parameters constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <iostream> using namespace std; class SetShowData { char someName[128]; int someAge; public: // члены класса и одноименные параметры: SetShowData(char someName[], int someAge) { strcpy_s(this->someName, someName); this->someAge = someAge; } void showData() { cout << "\n" << someName << " " << someAge << endl << endl; } }; int main() { setlocale(LC_ALL, "rus"); SetShowData objectOfClass("Stanislav", 34); objectOfClass.showData(); return 0; } |
In string 10 we have defined a constructor with parameters, whose names coincide with the names of members of the class: someName and someAge. Next to allow the compiler to understand what and where to copy, we explicitly use the pointer this:
13 | this->someAge = someAge; |
It means, in that the member of the classsomeAge you must write the value, which will set about creating a class object.
To summarize : Pointer this – this pointer, which stores the address of a particular object class. It is present in a hidden first parameter in each class method (except static methods). The type of the pointer is the class name. The class methods, if necessary, can be used this clearly. However, explicitly declare, to initialize or change the pointer, there is no possibility.
Hope more or less you understand with this theme. If you have questions – please comment.
And that turns out? Is this, but you can declare your pointer, and to use instead of this?
Declare a pointer, you can, but it will be full duplicate this, so no point in doing this is not.
But this without any other access to “this object” would not be.
Just, available. Thank you:)
good afternoon. Thank you very much for your example, prompt, there is an error on your code:
Ошибка (actively)E0289 There are no designer items “SetShowData::SetShowData”, appropriate argument list argument types: (const char [10], int). I am doing copy-paste. In what may be a mistake? Thank you.
Mine also throws an error. Mine also throws an error. Mine also throws an error
Why make a fuss?
SetShowData(char Name[], int Age)
{
strcpy_s(someName, Name);
someAge = Age;
}
Example, for examples.
Date& statement ++()
{
return *this;
}
What will happen in this case,?
Date& statement ++()
{
return *this;
}
What will happen in this case,?
writing on paper is cool