当前位置:网站首页>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;
}边栏推荐
- 25 openwrt guest network add
- 【sylar】框架篇-Chapter15-Stream 模块
- [record of question brushing] 9. Number of palindromes
- [mathematical modeling] Based on MATLAB seismic exploration Marmousi model [including Matlab source code, 1977]
- [blood vessel detection] Based on MATLAB mom method, combined with Hessian and curve fitting, blood vessel diameter measurement [including Matlab source code, 1970]
- 校园流浪猫信息记录和分享的小程序源码
- idea2022更改本地仓库,配置阿里云中央仓库
- 【sylar】框架篇-Chapter14-TcpServer 模块
- 上班摸鱼打卡模拟器微信小程序源码
- Bio annotation of emotion analysis aste triples extraction
猜你喜欢

Reading of the paper "attentional encoder network for targeted sentimental classification"

Citrix virtual desktop tcp/udp transmission protocol switching

Information system project manager (2022) - key content: information system integrated testing and management, project management maturity model, quantitative project management (21)

高数_第4章__曲线积分

Practice and thinking of AI standardization engine in pink client

Advanced architects, 16 common principles of microservice design and Governance

Null security and exception

《KG-BERT: BERT for Knowledge Graph Completion》

重要的 SQL Server 函数 - 数字函数
![[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)
随机推荐
Select sorting method
Study notes of Gu Yujia on July 27, 2022
Docking with Hang Seng express ― dolphin DB NSQ plug-in tutorial
Space complexity calculation super full sorting!! (calculation of hand tearing complexity
【实战】使用 Web Animations API 实现一个精确计时的时钟
High number_ Chapter 4__ Curvilinear integral_ Exercise solution
When import is introduced, sometimes there are braces, sometimes there are no braces. How should we understand this?
After login, the upper right corner changes to enter the login status
Slice切片
Power consumption: leakage power
Information system project manager (2022) - key content: organization level project management, process management, project set management (18)
CMake使用基础汇总
Idea2022 change the local warehouse and configure Alibaba cloud central warehouse
H. 265 web player easyplayer realizes webrtc video real-time recording function
空间复杂度计算超全整理!!(一起手撕复杂度计算
10 more advanced open source command line tools
Bio annotation of emotion analysis aste triples extraction
Reading of a unified generic framework for aspect based sentimental analysis
21 openwrt kernel module changed to.Ko automatic loading
[day03] process control statement