本人做c++开发,谁能把 inline、staitc、const修饰函数、修饰成员变量、修饰局部变量讲明白呢
不同修饰符号、编译后、运行时,所在的内存区域,最好能Demo验证!
inline:
inline 是一个函数修饰符,是用来请求编译器在调用处展开该函数的代码,并不是生成一个函数调用的机器代码。这养,就可以提高程序的执行速度了,但是会增加了程序的代码数量。
static:
static 是一个修饰符,可以用于修饰函数、成员变量和局部变量。当 static 修饰一个函数时,它的生存期与整个程序相同,当且仅当在定义它的文件内才能看到。当 static 修饰成员变量时,它是该类的类成员,并且与整个类共享一个实例。当 static 修饰局部变量时,它的生存期与整个程序相同,但也是当且仅当在定义它的代码块内才可以看见。
stcon:
const 是一个修饰符,可以用于修饰函数、成员变量、局部变量和指针。当 const 修饰一个函数时,它声明该函数不会修改对象的状态。当 const 修饰成员变量时,它声明该成员变量是一个常量,不能在类的其他成员函数中被修改。当 const 修饰局部变量时,它声明该变量是一个常量,不能被修改。当 const 修饰指针时,它声明该指针指向的对象是一个常量,不能通过该指针修改指向的对象的值。
const 修饰指针时,在下面的代码中看到编译器生成的错误信息:
#include <iostream>
int main() {
int x = 5;
const int *ptr = &x; // ptr 是一个指向 int 常量的指针
// 下面的语句将导致编译错误
// *ptr = 10;
std::cout << "x = " << x << std::endl;
return 0;
}
编译器下错误信息:
error: assignment of read-only location '*ptr'
说明不能通过该指针修改指向的对象的值。
代码验证 inline 函数:
#include <iostream>
inline int max(int a, int b) {
return a > b ? a : b;
}
int main() {
int a = 10, b = 20;
std::cout << "max(" << a << ", " << b << ") = " << max(a, b) << std::endl;
return 0;
}
看到以下输出:
max(10, 20) = 20
说明函数 max() 正确地返回了两个整数中的最大值。
以下代码验证 static 变量:
#include <iostream>
void incrementStatic() {
static int count = 0; // static 变量
++count;
std::cout << "count = " << count << std::endl;
}
int main() {
for (int i = 0; i < 5; ++i) {
incrementStatic();
}
return 0;
}
代码输出:
count = 1
count = 2
count = 3
count = 4
count = 5
说明 static 变量在函数被调用多次时仍然保留其值,而不是在每次调用时重新初始化。
赞
回复
“该回答引用ChatGPT”
请参考,觉不错还请 点击采纳,感谢!
inline:inline 修饰符告诉编译器在编译的时候尽可能的在调用的地方展开该函数的代码,以减少函数调用的开销。使用 inline 可以加快代码的执行速度。
inline int max(int a, int b) {
return (a > b) ? a : b;
}
static:static 修饰符可以用于函数和变量。当 static 修饰函数时,该函数仅在该文件内可见,不能在其他文件中访问;当 static 修饰变量时,该变量的生命周期与程序的生命周期相同,在程序结束时该变量才会消失。
// 修饰静态函数
static int count() {
static int c = 0;
return ++c;
}
// 修饰静态变量
static int x = 10;
const:const 修饰符用于声明常量,声明后该变量不可再被修改。当 const 修饰函数时,该函数不能修改类的成员变量。
// 修饰常量
const int size = 100;
// 修饰函数
void print(const int* arr, int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
}
对于修饰成员变量、局部变量,请查看相应代码块中的变量声明。
所有这些修饰符编译后都在代码段中,运行时可以存储在内存的栈区域或堆区
inline:内联函数是一种编译器优化技术,它允许编译器在调用函数时直接将函数的代码替换为调用代码。
staitc:修饰局部变量时,这个变量的生存期和作用域不受限制,也就是说在整个程序中可以访问。修饰成员变量时,该成员变量仅属于静态数据,在整个类中可以被访问,但不与对象绑定。
const:修饰函数时,函数不能修改对象的状态。修饰成员变量时,该成员变量为常量,不能被修改。修饰局部变量时,该局部变量也为常量,不能被修改。
C++ 的内存分为栈内存和堆内存。静态数据存储在静态存储区(数据段),栈内存存储函数的变量和局部变量,堆内存存储由new分配的动态数据。
#include <iostream>
inline int Max(int a, int b) { return a > b ? a : b; }
class Test
{
public:
static int s_value;
int m_value;
};
int Test::s_value = 0;
int main()
{
const int value = 10;
Test test;
test.m_value = 20;
Test::s_value = 30;
std::cout << Max(value, test.m_value) << std::endl;
std::cout << Test::s_value << std::endl;
return 0;
}
上面的代码演示了 inline、static、const 修饰符的使用。
"inline", "static", and "const" are three different type specifiers in C++ that can be used to modify functions and variables.
"inline" is a hint to the compiler to replace the function call with the body of the function at compile-time. This can reduce the overhead of function calls and improve performance, but too much inlining can increase the size of the compiled binary.
"static" can be used to modify either functions or variables. When applied to a function, it restricts the scope of the function to the current file, making it invisible to code outside of the file. When applied to a variable, it restricts the scope of the variable to the current block and causes the variable to retain its value between calls.
"const" is used to declare a variable or function that cannot be modified. When applied to a variable, it tells the compiler that the value of the variable cannot be changed. When applied to a function, it tells the compiler that the function cannot modify the values of any objects visible from the caller.
Here is an example that demonstrates the use of "inline", "static", and "const" together:
c
#include <iostream>
using namespace std;
inline static const int square(int x) {
return x * x;
}
int main() {
const int size = 100;
cout << square(5) << endl;
cout << square(size) << endl;
return 0;
}
This code defines an inline function "square" that calculates the square of its argument. The function is declared as both static and const, meaning that its scope is limited to the current file and that it cannot modify the values of any objects visible from the caller. When compiled and run, this code will output:
25
10000
In this example, the inline function "square" is replaced by its body at compile-time, resulting in faster execution. Additionally, the function is restricted to the current file and cannot modify any objects, ensuring that the program is safe and predictable.
"inline" in C++ is a keyword used to declare a function as an inline function, which suggests the compiler to replace calls to that function with the body of the function.
"static" in C++ can be used to declare a variable or function with static storage duration, meaning it has a single instance throughout the program, with its lifetime starting at program startup and ending at program termination. When used in a class, it means that the function or variable is shared among all objects of the class and is not associated with any specific object.
"const" in C++ is a keyword used to specify that a value cannot be modified. It can be applied to variables, functions, and member functions.
inline: suggest the compiler to inline a function for optimization
static: give a variable/function a single instance throughout the program, shared among all objects in a class
const: make a value unmodifiable.
参考一下吧,https://blog.csdn.net/cyjbj/article/details/126688934?spm=1001.2014.3001.5502
inline 修饰符主要是将函数体的代码拷贝到调用这个函数的地方,从而避免调用函数时出现函数调用的开销。它的作用范围是在编译器,编译后,运行时的内存区域是一个整体,不会分离出函数体的内存和调用函数的内存。Static 修饰符主要作用是提升变量或者函数的作用域,只能在本文件使用,当程序编译完成后,会在数据段中占用一块内存,运行时此内存和代码段一起存在,来保存变量的值。Const 修饰符主要用来修饰变量,将变量的值变为常量,无法被修改,在编译阶段就已经将常量的值替换到应用程序中,所以在编译完成后,运行时也是一个常量,这个常量被存储在代码段中,可以通过一个Demo来验证上述修饰符的作用。