What are the differences between struct and class in C++?

后端 未结 30 3747
盖世英雄少女心
盖世英雄少女心 2020-11-21 05:38

This question was already asked in the context of C#/.Net.

Now I\'d like to learn the differences between a struct and a class in C++. Please discuss the technical d

30条回答
  •  不要未来只要你来
    2020-11-21 05:57

    You might consider this for guidelines on when to go for struct or class, https://msdn.microsoft.com/en-us/library/ms229017%28v=vs.110%29.aspx .

    √ CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

    X AVOID defining a struct unless the type has all of the following characteristics:

    It logically represents a single value, similar to primitive types (int, double, etc.).

    It has an instance size under 16 bytes.

    It is immutable.

    It will not have to be boxed frequently.

提交回复
热议问题