当前位置:网站首页>13.inline,const,mutable,this,static
13.inline,const,mutable,this,static
2022-06-13 08:50:00 【zzyzxb】
One : Implementing member functions in class definitions inline: The implementation of a member within a class is also called a function within a class
This kind of member function implemented directly in the definition of class , Will be regarded as inline Inline functions to handle .
Two : At the end of a member function const
const : Constant , Add a after the member function const. Not only to add... In the member function declaration const , Also add to the member function definition const;
effect : Tell the system , This member function It will not modify the value of any member variable in the object, etc . in other words , This member function does not modify any state of the class .
Add one behind your ass const Suffix member function " Constant member functions ";
const time abc; Definition const object , This object has limitations .
abc.initMillTime(1500); Can not be ,initMillTime() Not at all const Member functions , This initMillTime() Can only be non const Object call .
time def;
def.initMillTime(1500);
const Member functions , No matter what const object , Or not const, Fine const, You can call const member .
Instead of const Member functions , Can't be const Object call , Can only be given const Object call .
void ptfunc() const
{
return;
}
Report errors ,const Can only load member functions , You can't add it to an ordinary function
3、 ... and :mutable( unstable , Easy to change ),const The antonym of .mutable The introduction of is just to break through const The limitation of .
use mutable Modify a member variable , Once a member variable is mutable modification . It means , This member variable is always in a state that can be modified ,
Even in the const In the member function at the end , You can also modify .
Four : Returns a reference to its own object ,this.
class time{
public:
int hour;
int minute;
int second;
static int mystatic; Declare static member variables , Memory has not been allocated yet , So you can't initialize here .
In another .cpp The document defines int time::mystatic = 15; Definition is not required static.
static void mstafunc(int testvalue);
In another .cpp The document defines void time::mstafunc(int testvalue){}
static Member functions cannot modify non members static Member variables , It can only be modified static Member variables of .
void initTime(int tmphour,int tmpmin, int tmpsec){
hour = tmphour;
minute = tmpmin;
second = tmpsec;
}
public:
time& add_hour(int tmphour){
hour += tmphour; this->hour += tmphour
return *this; Return the object itself
}
time& add_minute(int minute){
this->minute += minute;
return *this;
}
};
time mytime;
mytime.add_hour(3).add_minute(12);
How to understand this ( Reserved words ), When we call member functions , The compiler is responsible for the address of this object (&mytime) Passed to a hidden this Shape parameter .
From a system perspective , Direct access to class members is considered through this Implicitly called .
Internal practices of the compiler
time& add_hour(time *this, int tmphour){
this->hour += tmphour;
hour += tmphour;
return *this; Return the object itself
}
time mytime;
mytime.add_hour(&mytime,3);
(1)this Pointers can only be used in member functions , Global function , Static functions cannot be used this The pointer .
(2) In ordinary member functions ,this Is pointing to the non const Object's const The pointer ( The type is time, that this Namely time *const this), Express this Can only point to objects time Object call .
(3) stay const In member function , this The pointer is a point const Object's const The pointer ( The type is time,this Namely const time *const this Pointer to type )
5、 ... and :static member
There are member variables belonging to the entire class , This member variable is called static Member variables .
characteristic : Does not belong to an object , Belong to the whole class , Once we change the value of this member variable in an object , The result of the modification can be seen directly in other objects .
There is only one copy of this member variable , Reference to this member variable , We're going to use Class name :: Member variable name
Member functions can also be preceded by static constitute , Static member functions . Member functions belonging to the entire class . Invocation time Class name :: Member function name
How to define static Member variables ( Allocate memory )?
We usually go to a certain .cpp This static member variable is defined at the beginning of the source file . This ensures that the static member variable has been successfully initialized before calling any function .
void func(){
static int abc = 5; Local static variable
}
static int g_abc = 15; Also stored in static storage . Limiting this global quantity can only be used in this document .
extern int g_abc = 0;
Reference static member variables
cout << time::mystatic << endl;
time mytime1;
mytime1.mystatic = 40;
time mytime2;
cout << time::mystatic <<endl;
cout << mytime1.mystatic << endl;
cout << mytime2.mystatic << endl; Values are 40
time::mystatic(12);
mytime1.mstafunc(15);
mytime2.mstafunc(78);
边栏推荐
- I set up a blog
- DIY无人机(匿名拓控者P2+F330机架)
- Buuctf web (IV)
- filebeat采集日志到ELK
- Three methods to make the scroll bar of div automatically scroll to the bottom
- PHP wechat special merchant incoming V3 packaging interface
- Explanation of JS event loop mechanism and asynchronous tasks
- Namespace in TS (2)
- Invalid flex layout setting width
- 银行理财产品有哪些?清算期是多长?
猜你喜欢

redis. exceptions. ConnectionError: Error 111 connecting to 172.16.8.128:6379. Connection refused.

redis.exceptions.ConnectionError: Error 111 connecting to 172.16.8.128:6379. Connection refused.

Installing pytorch under Anaconda

Notes on development experience: TP5 exp query, SQL analysis, JQ, applet, right-click menu, Linux skills, shell skills, mysql, etc

VI editor

Buffer Overflow Vulnerability Lab

JS - array de duplication in the array object case

Buuctf web (VII)

1.SolidWorks各模块的学习顺序

d3.js&nvd3. JS - how to set the y-axis range - d3 js & nvd3. js — How to set y-axis range
随机推荐
1. Learning sequence of SolidWorks modules
[pychart pit stepping record] prompt after configuring remote operation, [errno 2] no such file or directory
0. Quelques doutes au sujet de SolidWorks
À propos des principes de chiffrement et de décryptage RSA
System analysis - detailed description
5、 Constant, variable
Mapbox loads nationwide and provincial range, displaying multi-color animation points, migration lines, 3D histogram, etc
Undefined and null in JS
容器概念和云原生
Namespace in TS (2)
Custom exception class myexception
Buuctf web (V)
Cesium view switching, locating, reading files, building data sources, entity control, model control, etc
Mobile terminal development I: basic concepts
JS wrapper object
Buuctf web (III)
0.一些自己初學Solidworks的疑惑
turf. JS usage
情绪处理技巧
Pop component submission success failure animation