C + + this pointer detailed __c++

Source: Internet
Author: User
Tags function definition

The this pointer can only be invoked in a member function of a class, which represents the address of the current object. Here is an example:

1
2
3
4
5
6
void Date::setmonth (int mn)
{
month = MN; These three sentences are equivalent.
This->month = MN;
(* this). Month = MN;
}

1. This can only be used in member functions.
Global functions, which cannot be used by static functions.
In fact, the default first argument for a member function is t* Const Register this.
Such as:
Class a{public:int func (int p) {}};
Among them, the Func prototype in the compiler should look like: int func (A * const register this, int p);

2. This shows that this is constructed before the start of the member function and is cleared after the end of the member.
This life cycle is the same as the parameters of any function, without any distinction.
When a member function of a class is invoked, the compiler passes the pointer of the class as the This parameter of the function. Such as:
A A;
A.func (10);
Here, the compiler will compile: A::func (&a, 10);
Well, it doesn't look like a static function, does it? However, the difference is still there. The compiler usually does some optimizations on this pointer, so the this pointer is more efficient--such as VC usually passes the this parameter through the ECX register.

3. Answer
#1: When was this pointer created?
This is constructed before the start of the member function, and is cleared after the execution of the member is completed.

#2: Where is this pointer stored? Heap, stack, global variable, or other?
The this pointer is placed in a different position because of the compiler. It could be a stack, it could be a register, or even a global variable.

#3: This pointer is passed to a function in a class. Binding? or the first argument of a function argument is the this pointer. So how does this pointer find a function after a class instance?
This is passed by the first argument of the function argument. The this pointer was generated before the call. The function after the class instance, does not have this argument. When instantiating a class, only the variable space in the class is allocated, and no space is allocated for the function. Since the function definition of a class has been completed, it is there and will not run.

#4: This pointer accesses the variable in the class/?
If it's not a class, but a structure, then how do you access the variables in the structure through the structure pointer? If you understand this, it's a good way to understand the problem.
In C + +, classes and structs are only one difference: the members of a class are private by default, and the structure is public.
This is the pointer to the class, and if you replace it with a struct, this is the pointer to the structure.

#5: We only get an object to use this pointer through an object, if we know the position of an object this pointer can be used directly?
The this pointer is only defined in member functions. Therefore, after you get an object, you cannot use the this pointer through the object. Therefore, we cannot know the position of this pointer of an object (only the position of this pointer in a member function). Of course, in a member function, you can know the position of this pointer (which can be obtained &this), or it can be used directly.

#6: Do you want to create a function table in a class to hold function pointers after each class is compiled so that you can invoke the function?
Normal class functions (whether they are member functions or static functions) do not create a function table to hold the function pointer. Only virtual functions are placed in the function table.

However, even if it is a virtual function, if the compiler knows exactly which function to call, the compiler will not invoke it indirectly through a pointer in the function table, but will call the function directly.

# 7: How do these compilers do it? 8: Can the simulation be realized?
Knowing the principle, these two questions are easy to understand.
In fact, the simulation implementation of this call, in many cases, many people have done.
For example, a system callback function. There are a lot of system callback functions, such as timing, thread, and so on.

example of a thread:

C + + Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Class A
{
int n;
Public
static void run (void *pthis)
{
A *this_ = (A *) pThis;
This_->process ();
}
void process () {}
};
Main ()
{
A A;
_beginthread (a::run, 0, &a);
}

Here is the definition of a static function to simulate member functions.

There are also many programs written in C that simulate the implementation of a class. such as FreeType library and so on.
In fact, people who have used C language, most of them have been simulated. But there was no clear concept at the time.
Such as:

C + + Code

1
2
3
4
5
6
7
8
9
typedef struct STUDENT
{
int age;
int no;
int scores;
} Student;
void Initstudent (Student *pstudent);
void Addscore (Student *pstudent, int score);
...

If you change the pstudent to this, it will be the same.

It is equivalent to:

C + + Code

1
2
3
4
5
6
7
8
9
Class Student
{
Public
int age;
int no;
int scores;
void Initstudent ();
void Addscore (int score);
}

The const constants can have physical storage space and therefore can be addressed

The this pointer is created before the object is created.
The this pointer is placed on the stack and is determined at compile time.
And there is only one this pointer when an object is created and runs throughout the program.

Related Article

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.