是否可以编写一个与C struct 完全兼容的C++ 类或结构.从兼容性我的意思是对象的大小和变量的内存位置.我知道使用 *(point*)&pnt
甚至 (float*)&pnt
(在变量是浮点数的不同情况下)是邪恶的,但请考虑出于性能考虑,它确实需要.每秒使用百万次常规类型转换运算符是不合逻辑的.
Is it possible to write a C++ class or struct that is fully compatible with C struct. From compatibility I mean size of the object and memory locations of the variables. I know that its evil to use *(point*)&pnt
or even (float*)&pnt
(on a different case where variables are floats) but consider that its really required for the performance sake. Its not logical to use regular type casting operator million times per second.
举个例子
Class Point {
long x,y;
Point(long x, long y) {
this->x=x;
this->y=y;
}
float Distance(Point &point) {
return ....;
}
};
C 版本是一个 POD 结构
C version is a POD struct
struct point {
long x,y;
};
是.
(编辑)
这篇关于C++ 类或结构与 C 结构的兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!