The basics of programming in c++ for beginners

This pointer C ++

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.

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.

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:

this pointer in c ++, this c++, this с++

Or so:

this pointer in c ++, this c++, this с++

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.

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:

 It means, in that the member of the classsomeAge you must write the value, which will set about creating a class object.

this pointer in c ++, this c++, this с++

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.

10 thoughts on “This pointer C ++

  1. And that turns out? Is this, but you can declare your pointer, and to use instead of this?

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

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

  3. Why make a fuss?
    SetShowData(char Name[], int Age)
    {
    strcpy_s(someName, Name);
    someAge = Age;
    }

Leave a Reply to Anonymous Cancel reply

Your email address will not be published. Required fields are marked *