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

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

    Class is only meaningful in the context of software engineering. In the context of data structures and algorithms, class and struct are not that different. There's no any rule restricted that class's member must be referenced.

    When developing large project with tons of people without class, you may finally get complicated coupled code because everybody use whatever functions and data they want. class provides permission controls and inherents to enhance decoupling and reusing codes.

    If you read some software engineering principles, you'll find most standards can not be implemented easily without class. for example: http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29

    BTW, When a struct allocates a crunch of memory and includes several variables, value type variables indicates that values are embbeded in where struct is allocated. In contrast, reference type variable's values are external and reference by a pointer which is also embedded in where struct is allocated.

提交回复
热议问题