当前位置:网站首页>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
边栏推荐
- Leetcode48. Rotate image
- Four characteristics and isolation levels of database transactions
- Lavel document reading notes -how to use @auth and @guest directives in lavel
- 基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 2
- From programmers to large-scale distributed architects, where are you (I)
- Rhcsa - day 13
- Latex insert picture, insert formula
- Ruby时间格式转换strftime毫秒匹配格式
- Latex error: missing delimiter (. Inserted) {\xi \left( {p,{p_q}} \right)} \right|}}
- Static comprehensive experiment ---hcip1
猜你喜欢
Two way process republication + routing policy
Dynamic memory management
Reasons and solutions for the 8-hour difference in mongodb data date display
Occasional pit compiled by idea
Rhcsa learning practice
OSPF summary
Recursion and divide and conquer strategy
【Day2】 convolutional-neural-networks
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 2
DML statement of MySQL Foundation
随机推荐
Press the button wizard to learn how to fight monsters - identify the map, run the map, enter the gang and identify NPC
Debug:==42==ERROR: AddressSanitizer: heap-buffer-overflow on address
Delayed message center design
RHCE day 3
[200 opencv routines] 218 Multi line italic text watermark
Latex learning insertion number - list of filled dots, bars, numbers
Devop basic command
Doris / Clickhouse / Hudi, a phased summary in June
MPLS: multi protocol label switching
Exercise 7-4 find out the elements that are not common to two arrays (20 points)
Rhcsa day 9
Software sharing: the best PDF document conversion tool and PDF Suite Enterprise version sharing | with sharing
Evolution from monomer architecture to microservice architecture
PHP代码审计3—系统重装漏洞
转载:等比数列的求和公式,及其推导过程
Button wizard business running learning - commodity quantity, price reminder, judgment Backpack
What is devsecops? Definitions, processes, frameworks and best practices for 2022
Rhsca day 11 operation
IPv6 comprehensive experiment
[FAQ] summary of common causes and solutions of Huawei account service error 907135701