当前位置:网站首页>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
边栏推荐
- RHCE day 3
- BGP ---- border gateway routing protocol ----- basic experiment
- AUTOSAR从入门到精通100讲(106)-域控制器中的SOA
- Pod management
- Today's sleep quality record 78 points
- 基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 2
- Time complexity and space complexity
- 【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
- Network disk installation
- If you don't know these four caching modes, dare you say you understand caching?
猜你喜欢

Three schemes of ZK double machine room

MPLS: multi protocol label switching

DML statement of MySQL Foundation

Sword finger offer 05 (implemented in C language)

Time complexity and space complexity

From programmers to large-scale distributed architects, where are you (2)

RHCE - day one

Vs201 solution to failure to open source file HPP (or link library file)

2. Data type

PHP code audit 3 - system reload vulnerability
随机推荐
Exercise 7-8 converting strings to decimal integers (15 points)
按键精灵跑商学习-商品数量、价格提醒、判断背包
Software sharing: the best PDF document conversion tool and PDF Suite Enterprise version sharing | with sharing
leetcode1-3
OSPF summary
Write a program to define an array with 10 int elements, and take its position in the array as the initial value of each element.
Rhcsa day 9
Lavel document reading notes -how to use @auth and @guest directives in lavel
Today's sleep quality record 78 points
Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 1
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Custom type: structure, enumeration, union
DDL statement of MySQL Foundation
Devop basic command
From programmers to large-scale distributed architects, where are you (2)
Map container
What is devsecops? Definitions, processes, frameworks and best practices for 2022
Basic principle of servlet and application of common API methods
/*The rewriter outputs the contents of the IA array. It is required that the type defined by typedef cannot be used in the outer loop*/