当前位置:网站首页>Introduction to this pointer
Introduction to this pointer
2022-07-28 04:30:00 【Magic tea】
Catalog
brief introduction :
C++ The compiler gives each “ Non static member functions “ Added a hidden pointer parameter
Count , Let the pointer point to the current object ( The object that the function calls at run time ), Operation of all member variables in the function body , All are accessed through this pointer . But all operations are transparent to users , That is, the user does not need to deliver , Compiler autocompletes
characteristic :
1. this The type of pointer : Class types * const
2. Only in “ Member functions ” Internal use of
3. this Pointer is essentially a parameter of a member function , When an object calls a member function , Pass the object address as an argument to this
Shape parameter . So objects don't store this The pointer .
4. this Pointer is the first implied pointer parameter of a member function , Generally, the compiler passes through ecx Register auto transfer , No need for users
Pass on
give an example :
class Data {
public:
void Printf()
{
cout << _year <<" "<<" "<< _month <<" "<< _day << endl;
}
void Init(int year=2022,int month=5,int day=25)
{
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Data d1,d2;
d1.Init(2022,1,1);
d1.Printf();
d2.Init(2022,2,2);
d2.Printf();
return 0;
} 
This is a simple date class , So here's a problem , We are using the print function Printf And initialization functions Init When ,d1 and d2 The same function is called , So how does the compiler know that I should set / Print d1 still d2 Well ?
This is actually used this The pointer
So how does the specific compiler do it ?
void Printf(const* this)// The compiler actually handles
{
cout << this->_year << " " << this->_month << " " << this->_day << endl;
}
void Printf()// What we see
{
cout << _year <<" "<<" "<< _month <<" "<< _day << endl;
}void Init(const* this,int year=2022,int month=5,int day=25)// The compiler handles
{
this->_year = year;
this->_month = month;
this->_day = day;
}
void Init(int year = 2022, int month = 5, int day = 25)// What we see
{
_year = year;
_month = month;
_day = day;
}d1.Init(2022,1,1);// What we see
d1.Init(&d1,2022, 1, 1);// The compiler actually handles In fact, the Editor takes d1 and d2 Address of function , And passed it on to const*this, In this way, the compiler can automatically print and initialize the corresponding structure .
Be careful :
We can't add const*this and &, This is what the compiler does , We can't rob the compiler , Even if I did , Compilation will not pass , But we can add
void Printf(const* this)// error
{
cout << _year <<" "<<" "<< _month <<" "<< _day << endl;
}
void Printf()// Can run , But the compiler will add this->, So we don't need to add , It's okay to add it
{
cout <<this-> _year <<" "<<" "<< this->_month <<" "<< this->_day << endl;
}边栏推荐
- transform: failed to synchronize: cudaErrorAssert: device-side assert triggered
- MATLB | location and constant volume IEEE30 node implementation of distributed energy
- Idea start project MVN command terminal cannot recognize "MVN" item as cmdlet
- 【sylar】框架篇-Chapter9-hook 模块
- Reading of seq2path: generating sentimental tuples as paths of a tree
- 【sylar】框架篇-Chapter21-环境变量模块
- 重要的 SQL Server 函数 - 字符串实用程序
- Full resolution of the use of go native plug-ins
- Information system project manager (2022) - key content: Project Contract Management (13)
- [blood vessel detection] Based on MATLAB mom method, combined with Hessian and curve fitting, blood vessel diameter measurement [including Matlab source code, 1970]
猜你喜欢

Study notes of Gu Yujia on July 27, 2022

IPC: multi process binder Aidl user guide, including cases

Full resolution of the use of go native plug-ins
![[performance optimization methodology series] III. core idea of performance optimization (2)](/img/3c/9a2daea9bc136ae48d20d96e8f0402.png)
[performance optimization methodology series] III. core idea of performance optimization (2)

Null security and exception

How to select reliable securities analysts?

2022-7-27 顾宇佳 学习笔记

How to upgrade a pair of 12.2 RAC(primary) and a pair of 12.2 RAC(dataguard) to 19c

MATLB | location and constant volume IEEE30 node implementation of distributed energy

could only be written to 0 of the 1 minReplication nodes. There are 0 datanode(s) running and 0 node
随机推荐
bubble sort
Use Baidu developer tool 4.0 to build a dedicated applet IDE
Cloud native Devops status survey questionnaire solicitation: kodelurover launched jointly with oschina
Study notes of Gu Yujia on July 27, 2022
Information system project manager (2022) - key content: Strategic Management (17)
MySQL: data types and operators
H265/hevc noun explanation -- CTU, CTB, Cu, CB, Tu, PU, TB, Pb, LCU, slice, tile, chroma, luma, I frame, B frame, P frame
20-Openwrt crond crontab
Slice slice
Go结构体
【sylar】框架篇-Chapter15-Stream 模块
功耗:Leakage Power
《关于我写自定义cell这件事》
Information system project manager (2022) - key content: Project Contract Management (13)
DNS series (III): how to avoid DNS spoofing
RuntimeError: stack expects each tensor to be equal size, but got [8] at entry 0 and [2] at entry 2
【sylar】框架篇-Chapter14-TcpServer 模块
【sylar】框架篇-Chapter12-ByteArray 模块
Information system project manager (2022) - key content: Project Risk Management (11)
仿真测试断开服务器公网连接