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

后端 未结 30 3056
盖世英雄少女心
盖世英雄少女心 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:54

    One other thing to note, if you updated a legacy app that had structs to use classes you might run into the following issue:

    Old code has structs, code was cleaned up and these changed to classes. A virtual function or two was then added to the new updated class.

    When virtual functions are in classes then internally the compiler will add extra pointer to the class data to point to the functions.

    How this would break old legacy code is if in the old code somewhere the struct was cleared using memfill to clear it all to zeros, this would stomp the extra pointer data as well.

    0 讨论(0)
  • 2020-11-21 05:55

    There are 3 basic difference between structure and class

    1St- memory are reserved for structure in stack memory (which is near to programming language )whether for class in stack memory are reserved for only reffrence and actual memory are reserved in heap memory.

    2Nd - By default structure treat as a public whether class treat as a private .

    3Rd- can't re -use code in structure but in class we can re-use same code in many time called inhertence

    0 讨论(0)
  • 2020-11-21 05:56

    Not in the specification, no. The main difference is in programmer expectations when they read your code in 2 years. structs are often assumed to be POD. Structs are also used in template metaprogramming when you're defining a type for purposes other than defining objects.

    0 讨论(0)
  • 2020-11-21 05:56

    The other answers have mentioned the private/public defaults, (but note that a struct is a class is a struct; they are not two different items, just two ways of defining the same item).

    What might be interesting to note (particularly since the asker is likely to be using MSVC++ since he mentions "unmanaged" C++) is that Visual C++ complains under certain circumstances if a class is declared with class and then defined with struct (or possibly the other way round), although the standard says that is perfectly legal.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 05:57

    I found an other difference. if you do not define a constructor in a class, the compiler will define one. but in a struct if you do not define a constructor, the compiler do not define a constructor too. so in some cases that we really do not need a constructor, struct is a better choice (performance tip). and sorry for my bad English.

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