Native use of C ++, this pointer

Possible duplicate:
When should I use the this pointer explicitly?

I am wondering how to use the "this" pointer correctly.

I saw someone create a class constructor with the argument passed to the variable passed to named 'data'. However, it had a private member variable named "data", so it just used:

this->data = data;

could just use

data = data_in

(if the parameter was named data_in), and there is no need to call the pointer "this" and refer to the type of element.

Now I wonder is this the right use? Using this->member reduce naming complexity? I mean, this works, and I see that it does what it was intended to do, but I wonder if some of you more experienced C ++ guys and girls can say a few words if this is common practice?

Also, out of curiosity, I measured the code to see what was going on under the hood, and it seemed that the "this" pointer was being called anyway. I assume that the path to the class object is still executing.

+4
source share
4 answers

In most cases, unnecessary disclosure of the this pointer to access the non-static data member of the class instance is optional, but it can help with naming confusion, especially when the members of the class data are defined in a separate header file from the code module. However, you must use the this pointer if you are accessing a non-static data member, which is a member of the base class, which is a template. In other words, in a situation such as:

 template<typename T> class base_class { protected: int a; }; template<typename T> class derived_class : public base_class<T> { void function() { a = 5; //this won't work this->a = 5; //this will work } }; 

you'll notice that you must use the this pointer to properly resolve the inherited non-static data member from the template base class. This is due to the fact that base_class<T>::a is a dependent name, in this case depending on the template parameter T , but when used without the this pointer, it is considered as an independent name and, therefore, cannot be viewed in the dependent namespace of the base class . Thus, without specifically dereferencing the this pointer, you will get a compiler error, for example, β€œ a not been declared in this area” or something like that.

+6
source

No access to class member like:

 this->member = something; 

similar to accessing an element in and of itself,

 member = something; 

The compiler sees it as the same, and there is no overhead. Even if you use the second format, the compiler does the same as the first format under the hood.

In short, trying to use either of the two formats to improve performance is futile. Of course, you may have to use the first format in some template cases (access to non-stationary data members of the template base class), but this is not for performance.

+2
source

Essentially, this is identical, with the exception of what Jason pointed out.

The nice part is that if you use this-> , most editors will fill in the code for you, while preserving the text.

0
source

This is β€œcorrect,” but it is also bad practice. Naming parameters similar to class members is a bad idea. Deliberately creating name conflicts is a bad idea.

Copy operators are intended for cases when name conflicts are unavoidable, for example, when redefining a base element or when using two libraries with identifier names that are independently selected. Do not consider them a free license to do stupid things.

If you do not practice chartered competition. Then, by all means, introduce name collisions.

0
source

Source: https://habr.com/ru/post/1381321/


All Articles