简体   繁体   中英

Pragma in c++ struct in class

I have one question about using pragma in c++ classes. 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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