c++ 11 call once
时间: 2024-01-06 19:03:35 浏览: 36
"Call once" typically refers to a programming concept where a function is only executed once, even if it is called multiple times. This is often used for initialization or setup functions.
In C, this can be achieved using static variables. Here's an example:
```
void initialize() {
static int initialized = 0;
if (!initialized) {
// do initialization here
initialized = 1;
}
}
int main() {
// call initialize() multiple times, but it will only execute once
initialize();
initialize();
initialize();
return 0;
}
```
In this example, the `initialized` variable is declared as static, which means it will retain its value between function calls. The first time `initialize()` is called, `initialized` will be 0, so the initialization code will run and `initialized` will be set to 1. On subsequent calls, `initialized` will already be 1, so the initialization code will not run.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)