C++ 中的 POD 类型
C++ 中的 POD 代表普通旧数据。 它是一个用关键字 struct
或 class
定义的类,只有 int、char、double、bool、signed/unsigned、long/short、float 等数据成员。
C++ 中的 POD 类型
我们知道,POD 通常是内置数据类型,例如类和结构,您可以使用关键字 class 或 struct 来定义,但它与其他类或结构不同。 C++ 中的 POD 不支持构造函数、析构函数、虚函数等。
C++ 中的 POD(Plain Old Data)是一个聚合类或结构,仅包含 POD 作为数据成员。 它不定义用户定义的复制赋值运算符或指向成员类型的指针的其他非静态成员。
示例代码:
#include<iostream>
using namespace std;
// POD (Plain Old Data)
struct Add { // defined with the keyword struct
int x;
int y;
};
int main() {
struct Add a;
a.x = 1;
a.y = 2;
cout<<"x = " <<a.x<< endl << "y = "<<a.y<< endl;
cout<<"Sum of x and y = "<< a.x + a.y<<endl;
std::cout << std::boolalpha;
std::cout << std::is_pod<Add>::value << '\n'; // this is to check POD,
// it returns Boolean value
return 0;
}
输出:
x = 1
y = 2
Sum of x and y = 3
true
这段代码有一个使用关键字 struct
定义的 ADD 类,并且有两个数据成员 - x 和 y。 值通过 main 函数中的类对象传递给 ADD 类成员。
这些值将被进一步添加并显示为 cout
语句中的输出。 现在,类已准备就绪,并且值已传递。
是时候检查它是否是 POD 了; is_pod 函数可以帮助我们。 如果该类是 POD,则返回 True,否则返回 False。
此外,指针,包括指向成员的指针和指向函数的指针,都是POD。 Enum、const 和 volatile 也是 POD。
POD 的结构、类或联合也是 POD,前提是所有非静态数据成员都指定为公共且没有虚函数、构造函数或析构函数以及基类。
相关文章
Arduino 复位
发布时间:2024/03/13 浏览次数:315 分类:C++
-
可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。
Arduino 的字符转换为整型
发布时间:2024/03/13 浏览次数:181 分类:C++
-
可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。
Arduino 串口打印多个变量
发布时间:2024/03/13 浏览次数:381 分类:C++
-
可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。