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

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

    It's worth remembering C++'s origins in, and compatibility with, C.

    C has structs, it has no concept of encapsulation, so everything is public.

    Being public by default is generally considered a bad idea when taking an object-oriented approach, so in making a form of C that is natively conducive to OOP (you can do OO in C, but it won't help you) which was the idea in C++ (originally "C With Classes"), it makes sense to make members private by default.

    On the other hand, if Stroustrup had changed the semantics of struct so that its members were private by default, it would have broken compatibility (it is no longer as often true as the standards diverged, but all valid C programs were also valid C++ programs, which had a big effect on giving C++ a foothold).

    So a new keyword, class was introduced to be exactly like a struct, but private by default.

    If C++ had come from scratch, with no history, then it would probably have only one such keyword. It also probably wouldn't have made the impact it made.

    In general, people will tend to use struct when they are doing something like how structs are used in C; public members, no constructor (as long as it isn't in a union, you can have constructors in structs, just like with classes, but people tend not to), no virtual methods, etc. Since languages are as much to communicate with people reading the code as to instruct machines (or else we'd stick with assembly and raw VM opcodes) it's a good idea to stick with that.

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

    0 讨论(0)
  • 2020-11-21 06:00

    Class' members are private by default. Struct's members are public by default. Besides that there are no other differences. Also see this question.

    0 讨论(0)
  • 2020-11-21 06:00

    According to Stroustrup in the C++ Programming Language:

    Which style you use depends on circumstances and taste. I usually prefer to use struct for classes that have all data public. I think of such classes as "not quite proper types, just data structures."

    Functionally, there is no difference other than the public / private

    0 讨论(0)
  • 2020-11-21 06:00

    Here is a good explanation: http://carcino.gen.nz/tech/cpp/struct_vs_class.php

    So, one more time: in C++, a struct is identical to a class except that the members of a struct have public visibility by default, but the members of a class have private visibility by default.

    0 讨论(0)
  • 2020-11-21 06:00
    • . In classes all the members by default are private but in structure members are public by default.

      1. There is no term like constructor and destructor for structs, but for class compiler creates default if you don't provide.

      2. Sizeof empty structure is 0 Bytes wer as Sizeof empty class is 1 Byte The struct default access type is public. A struct should typically be used for grouping data.

      The class default access type is private, and the default mode for inheritance is private. A class should be used for grouping data and methods that operate on that data.

      In short, the convention is to use struct when the purpose is to group data, and use classes when we require data abstraction and, perhaps inheritance.

      In C++ structures and classes are passed by value, unless explicitly de-referenced. In other languages classes and structures may have distinct semantics - ie. objects (instances of classes) may be passed by reference and structures may be passed by value. Note: There are comments associated with this question. See the discussion page to add to the conversation.

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