当前位置:网站首页>Const's constant member function after the function; Form, characteristics and use of inline function
Const's constant member function after the function; Form, characteristics and use of inline function
2022-07-04 10:30:00 【Big bear loves to work】
1 const Decorated constant member functions and const Class object
class Person{
//...
public:
int age() const; // Constant member function
int score(); // Non member function
private:
int m_Age;
int m_Score;
//...
}
- As mentioned above age() Function is a Constant member function , Constant member functions have the following characteristics
- Express You cannot change the value of a member variable Function of
- In constant member functions Only constant member functions can be called , Because very member functions , It is possible to change the value of member variables
- const Class objects can only call constant member functions in a class
- Because since the class object is already const 了 , If you call a non member function , It is possible to change the content of the class , This is related to const Class object contradiction
bool cmp(const Person& a, Person& b) {
return a.age() > b.age(); // Compiler yes
// return a.score() > b.score(); // Compiler error , Because with const Class object called non member function
}
// among a yes const Class object , Only constant member functions can be called
- For ordinary class objects , Then whether it is a constant member function or not , You can call
2 Inline function
// The first inline function
class Person{
//...
public:
inline( Not to add ) int age() const { return m_Age; } // Constant member function and inline function
int score(); // Nonmember functions
private:
int m_Age;
int m_Score;
//...
}
// The second kind of inline function
inline void myFun()
{
//....
//....
}
- The above code shows Two expressions of inline functions
- Write the member function implemented by the function directly in the definition of the class , Whether you add keywords or not inline Are all inline functions
- Ordinary function definition , Add keywords before its functions inline, It becomes an inline function
- The function of inline function
- In order to solve the efficiency problem of function call in the program
- The essence is , In the compilation stage, the compiler replaces the call expression of the inline function in the program with the function body of the inline function , Used to avoid calling this function at run time , This saves the overhead of calling functions at runtime ( For example, context saving , Copy arguments, etc )
- Points for attention of inline functions
- Loop statements and switch statements cannot be used in inline functions
- Generally, they are very short functions ( Such as 10 Within the line )
- The definition of an inline function must appear before the first call
边栏推荐
- Rhcsa operation
- Button wizard business running learning - commodity quantity, price reminder, judgment Backpack
- [200 opencv routines] 218 Multi line italic text watermark
- Seven examples to understand the storage rules of shaped data on each bit
- Four characteristics and isolation levels of database transactions
- Occasional pit compiled by idea
- Student achievement management system (C language)
- What is an excellent architect in my heart?
- 用数据告诉你高考最难的省份是哪里!
- For programmers, if it hurts the most...
猜你喜欢
随机推荐
AUTOSAR from getting started to mastering 100 lectures (106) - SOA in domain controllers
六月份阶段性大总结之Doris/Clickhouse/Hudi一网打尽
Doris / Clickhouse / Hudi, a phased summary in June
Use the data to tell you where is the most difficult province for the college entrance examination!
Vanishing numbers
Write a program that uses pointers to set all elements of an int array to 4.18: 0.
Write a program to judge whether the elements contained in a vector < int> container are 9.20: exactly the same as those in a list < int> container.
Sword finger offer 05 (implemented in C language)
system design
uniapp---初步使用websocket(长链接实现)
Exercise 9-3 plane vector addition (15 points)
Summary of several job scheduling problems
RHCE - day one
uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示
Reprint: summation formula of proportional series and its derivation process
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 2
How to teach yourself to learn programming
按键精灵打怪学习-识别所在地图、跑图、进入帮派识别NPC
Huge number (C language)
Recursive method to achieve full permutation (C language)








