C++ Class or Struct compatiblity with C struct

前端 未结 7 925
悲哀的现实
悲哀的现实 2020-12-10 07:36

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 k

相关标签:
7条回答
  • 2020-12-10 08:31

    In addition to other answers, I would be sure not to put any access specifiers (public:, private: etc) into your C++ class / struct. IIRC the compiler is allowed to reorder blocks of member variables according to visibility, so that private: int a; pubic: int b; might get a and b swapped round. See eg this link: http://www.embedded.com/design/218600150?printable=true
    I admit to being baffled as to why the definition of POD does not include a prohibition to this effect.

    0 讨论(0)
提交回复
热议问题