2
這個簡單類的類成員是否在內存中連續?具有特徵矩陣的類的連續數據?
class A{
A(){ /* somecode */}
~A();
Eigen::Matrix<5,1,double> a;
Eigen::Matrix<9,1,double> b;
};
std::vector<A> vec(10);
char * p = interprete_cast<char*>(&vec[0]);
// the pointer p can now continously access all "a" and "b" in continous order -> (a,b,...,a,b)
// For example write all data to a binary file!
file.write(p, sizeof(A)*vec.size())
在其他帖子中提到了關於#pragma pack
等的內容嗎? 這是需要嗎? Link
還是不如成員打包成一個struct
非常感謝:-)
你可以做一個簡單的測試:比較'sizeof(A)'和'sizeof(A :: a)+ sizeof(A :: b)'。如果這些值不等於,則在某處存在填充。 – Michael