extern bool
时间: 2023-07-26 14:23:31 浏览: 77
`extern bool` 是一个变量声明,用于声明一个布尔类型的外部变量。`extern` 关键字表示该变量在其他文件中定义,但在本文件中使用。
例如,在文件 A.cpp 中定义一个布尔类型的变量 `isOK`,并在文件 B.cpp 中使用它,可以这样写:
A.cpp 文件:
```
bool isOK = true; // 定义布尔类型的变量 isOK,并初始化为 true
```
B.cpp 文件:
```
extern bool isOK; // 在 B.cpp 中声明 isOK 为一个在其他文件中定义的布尔类型变量
if (isOK) {
cout << "isOK is true" << endl;
} else {
cout << "isOK is false" << endl;
}
```
这样,在编译时,编译器会将两个文件一起编译,将 A.cpp 中定义的变量 `isOK` 和 B.cpp 中声明的变量 `isOK` 连接起来。然后在运行时,B.cpp 中的代码就可以使用 A.cpp 中定义的变量 `isOK` 了。
使用 `extern` 关键字声明外部变量有助于在多个文件中共享变量,并提高代码的可维护性和可重用性。
相关问题
extern bool可以吗
可以使用`extern bool`来声明外部函数。在C#中,`extern`关键字用于声明一个外部函数,该函数在其他地方实现。通过使用`DllImport`特性,可以将外部函数与C#代码进行绑定。下面是一个示例:
```csharp
using System;
using System.Runtime.InteropServices;
public class Program
{
[DllImport("User32")]
public static extern bool FlashWindow(IntPtr hWnd, bool bInvert);
public static void Main()
{
// 调用外部函数
IntPtr hWnd = IntPtr.Zero; // 窗口句柄
bool bInvert = true; // 是否反转窗口
bool result = FlashWindow(hWnd, bInvert);
Console.WriteLine("FlashWindow result: " + result);
}
}
```
在上面的示例中,我们声明了一个名为`FlashWindow`的外部函数,并使用`DllImport`特性将其与`User32`库进行绑定。然后,在`Main`方法中调用了`FlashWindow`函数,并传递了窗口句柄和是否反转窗口的参数。最后,我们打印了`FlashWindow`函数的返回结果。
extern BOOL AUTOSTOP_ENABLE;
根据提供的引用内容,可以看出这是一个外部变量的声明。`extern BOOL AUTOSTOP_ENABLE;`的意思是声明了一个名为`AUTOSTOP_ENABLE`的BOOL类型的外部变量。这个变量可以在其他文件中定义,然后在当前文件中使用。这种方法可以在多个文件中共享变量,从而避免了重复定义和内存浪费的问题。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.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)