我想知道,如果我拿一个完全静态的类的地址,我保证其成员按正确的顺序排列吗?我想这也适用于在全局范围内定义的一组变量。
struct Buffer1
{
static float val1;
static float val2;
};
void readBuffer(void* buffer){ /*Do something*/}
int main()
{
// Sending the buffer
readBuffer(&Buffer1); // Type name is not allowed
readBuffer(&Buffer1::val1); // Does work, wondering if data will be correct
return 0;
}
另外,有没有办法获取一组静态或全局变量的大小?像:
// Getting the size of the buffer
size_t sizeOfBuffer = sizeof(Buffer1); // Wrong size, doesn't measure static members
sizeOfBuffer = sizeof(Buffer1::val1) + sizeof(Buffer1::val2); // Does work but doesn't account for data padding/alignment, and is also tedious to change
我知道在这种情况下,实例化课程会更好,但我对这两个问题感到好奇。感谢。
答案 0 :(得分:2)
不,C ++标准没有在静态范围内指定内存中对象的顺序。
C ++标准也不保证类成员的相对顺序。