简体   繁体   English

类中C ++ struct中的实用程序

[英]Pragma in c++ struct in class

I have one question about using pragma in c++ classes. 我有一个关于在c ++类中使用编译指示的问题。 I've read there ( Use of #pragma pack on a class ) that use pragma around c++ class is not recommended, but can I use pragma like this: 我读过这里(不建议在类上使用#pragma pack ),不建议在c ++类周围使用pragma,但是我可以这样使用pragma:

class TestClass {
  public:
    ConfigProtocol();

    #pragma pack(1)
    struct t_config_header {
      quint8 version;
      quint8 da;
      quint16 sa;
      quint16 counter;
    };
    #pragma pack()

};

will not it be mistake? 会不会是错误的?

Short answer: Yes you can (and in your case, as it seems you are implementing a communcation protocol, in fact, should) do it. 简短的回答:是的,您可以(并且在您的情况下,似乎正在实施通信协议,实际上应该这样做)。

The way you are using the pragma is only affecting the structure which invalidates the arguments made in the StackOverflow answer you are linking to: The struct is not significantly changing as long as it stays a struct. 您使用编译指示的方式只会影响使链接到的StackOverflow答案中的参数无效的结构:只要保留结构,该结构就不会发生重大变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM