Variable Sized Struct C++

前端 未结 10 1097
忘掉有多难
忘掉有多难 2020-11-30 04:48

Is this the best way to make a variable sized struct in C++? I don\'t want to use vector because the length doesn\'t change after initialization.

struct Pack         


        
10条回答
  •  渐次进展
    2020-11-30 05:28

    You can use the "C" method if you want but for safety make it so the compiler won't try to copy it:

    struct Packet
    {
        unsigned int bytelength;
        unsigned int data[];
    
    private:
       // Will cause compiler error if you misuse this struct
       void Packet(const Packet&);
       void operator=(const Packet&);
    };
    

提交回复
热议问题