C ++ this Detailed

  Before a lot of misunderstanding on this indicator, where a separate write to summarize, there are imperfect, please correct me criticism!

First, the problem

1. A class of different objects when you call their member functions, in fact, they call function is the same piece of code, member functions How do I know which object data members to access it?

  Yes, that through this pointer. Each object has a pointer to this, the memory address of an object this pointer record, when we call a member function, member function default first parameter T * const register this, most compilers pass this pointer by ecx register by the implicit this parameter can access the data members of the object.

2. Why is the class member functions and can not be modified by the static const?

  Const class of functions used to prevent modification of data members typically modify the object, but no static function is this pointer can not access the data members of the object.

Two, this pointer Precautions

1.C ++ in this key is a pointer to a constant pointer to the object's own, can not give this assignment;

2. Only members have this function pointer, a friend function is not a class member function, no this pointer;

3. Also this is no static function pointer, static functions as static variables, which does not belong to a particular object;

4.this pointer within the scope class member function, can not get out of the class;

5.this not part of the object pointer, this pointer is not occupied by the memory size on the reaction sizeof operator.

Three, this pointer using

1. Returns the class object itself is in a non-static member function of time, directly return * this, such as the default address-operator overloading function classes, also possible to return * refer to this, which can as input and output streams as a "cascade" operation;

2. Modify the class member variables and member variable or parameter names are the same, such as this-> a = a (a = a compiler written, however);

3. When the class definitions to use their own type variables, because at that time did not know the variable name, use this pointer to such use variable itself.

Four, this pointer explore

1.this pointer is created when? 

  The new object is created during the specific stage which needs further understanding.

2. this pointer is stored where? Heap, stack, global variables, or something else?

  this pointer because of different compilers have different placement. May be the stack, it may be a register, even global variables. In the compilation of which level, a value only appears in three forms: immediate, register values ​​and a memory variable. Is not stored in the register is stored in memory, and they are not high-level language variables corresponding.

3. Why C ++ NULL object pointer can call non-virtual member functions, and Java, but it's not?

  C ++ language is statically bound, which is a C ++ and Java to a significant difference. Member function is not tied to a particular object, functions common to all members of the body of a member function, when the program is compiled, i.e., address of the member function has been determined. In addition, C ++ only care about your pointer type, does not care whether the pointer points to a valid object, C ++ requires the programmer to ensure the effectiveness of their own hands. Moreover, in some systems, address 0 is valid, in theory, can construct a 0 on the address of the object, so the object nullptr in C ++ member function there is no reason to call.

  When nullptr object calls member functions, you do not access the object's unique memory part, the program running, because it would not use this, once the access member variables of this object, the program crashes. Of course nullptr calling virtual methods will not run with the (virtual table with a virtual function, use memory space), a virtual method calls are dependent on this pointer. Can be understood, you pass it a parameter error, but did not use the argument inside the function, so it does not affect the operation of the function. Refer to the following codes:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class CPeople
 5 {
 6 public:
 7     CPeople(const std::string& name, int age)
 8       : mName(name), mAge(age){}
 9      ~CPeople();
10  
11     void Print()
12     {
13         std::cout << "show people info:" std::endl;
14     }
15 
16     void PrintInfo()
17     {
18         std::cout << "name:" << mName << std::endl;
19         std::cout << "age:" << mAge << std::endl;
20     }
21  
22 private:
23  
24     std::string mName;
25     int mAge;
26  
27 };
28  
29 int main()
30 {
31     CPeople* jon = NULL;
32     jon-> the Print ();   // program running 
33 is      jon-> the PrintInfo ();   // crashes, an illegal address access, and this time mName mAge not allocated space 
34 is      return  0 ;
 35 }

 

V. Summary

  A classic pointer on this answer:

  When you enter a house,
  You can see tables, chairs, floors,
  But the house you can not see the whole picture.
  For instance of a class, the
  You can see it's member functions, member variables,
  But the example itself?
  this is a pointer, it always points you in this instance itself.

  After this pointer with a comprehensive understanding of C ++ class to understand whether more profound step? Slowly accumulated, profound knowledge, come on!

 

 

 

Guess you like

Origin www.cnblogs.com/yuwanxian/p/10988736.html