Difference between this pointer and m_hwnd

Source: Internet
Author: User

M_hwnd


① M_hwnd, a member variable, was first defined in the cwnd class and the first data member of the cwnd class,


Let's take a look at the msdn analysis:


The handle of the Windows window attached to this cwnd. The m_hwnd data member is


Public variable of Type hwnd.


It is a handle of the window class. Any class derived from cwnd has this handle.


This handle is also available inside the Defined Object of the generated class. It is the handle of the class or the object that identifies itself.


Every window has a handle to identify itself. In the cwnd class, this handle is directly encapsulated as a member variable.


So the member functions of the cwnd class do not have the handle parameter. For example: showwindow (hwnd ),


In the cwnd class or derived class, this function has no parameters. In fact, this function implements


It is easy to call: showwindow (hwnd), because it has been encapsulated in the class, so you do not need


The parameter is being passed.


 


② How can I obtain the handle of the window class? There are the following methods:


1. This-> m_hwnd;


2. getsafehwnd ();


3. afxgetmainwnd ()-> m_hwnd;


 


This


① For a class member function, not an object corresponds to a separate member function body, but all


Like sharing this member function body, that is, calling the same code segment


When a non-static member function is called for an object, the compiler automatically assigns the object address to the member function


This pointer is passed as an implicit parameter to the member function. Then, each non-static member function accesses non-static data.


When the this pointer is implicitly used, it is converted to this-> data member.


. After the program is compiled, the member function address is confirmed.


Yes. The reason why a member function can differentiate the data of various objects of this class is that it relies on the this pointer, that is


In each non-static member function of a class, there is an implicit this pointer, which points to the real


Example object.


 


② The this pointer of an object is not part of the object itself and does not affect the result of sizeof ("object"); it is


Special pointers hidden in member functions of each class. It points to the object being operated by the member function.



③ In C ++, The this pointer is implicitly declared as x * const this, which means that this pointer cannot be assigned;


In the const member function of the class, the type of this pointer is const x * const, which indicates


It is unchangeable (that is, the assignment operation cannot be performed on the data members of such objects );


④ Because this is not a common variable, the address of this cannot be obtained.


 


⑤ Explicitly referencing this pointer:


1. directly use


Return * this;


2. To avoid assigning values to the same object, for example, when the operator "=" is overloaded, for example:


Classtext & operator = (const classtext & instance)


{


If (this = & instance) {return * this;


}


M_nsize = instance. m_nsize;


If (null! = M_pbuffer ){


Delete [] m_pbuffer;


}


M_pbuffer = new char [max_path];


If (null! = M_pbuffer ){


Strncpy (m_pbuffer, instance. m_pbuffer, max_path );


}


Return * this;


}


3. When the formal parameter and the member variable name are the same, as shown in figure


This-> N = N (cannot be written as n = N); otherwise,


Assign values to variables;


 


Of course, you do not need to use this to access static members. You only need to add the class before the function name.


This is why static functions cannot access non-static members (no object pointer, how to access


Its member variable ?), Static member functions can naturally access static member variables. For example: static member variables are actually


It is a global variable with access restrictions. To access static member variables from outside the class, you must add the class name limitation before.


.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.