Inquiry about class variable declarations in C++

前端 未结 6 1172
悲&欢浪女
悲&欢浪女 2021-01-19 00:43

I have a class to represent a 3D vector of floats:

class Vector3D
{
    public:

    float x, y, z;
    float * const data;

    Vector3D() : x(0.0), y(0.0),         


        
6条回答
  •  [愿得一人]
    2021-01-19 01:39

    Yes. This class is layout-compatible standard-layout, because:

    • You have no virtual functions.
    • All data members are in a single access specifier block (the public:)

    Because of this, it's guaranteed to be laid out sequentially just like a C structure. This is what allows you to read and write file headers as structures.

提交回复
热议问题