Let's discuss the difference between the structure (struct) and the class. What do you think is the role of the structure (struct)? Can it be completely replaced by the class ??
---------------------------------------------------------------
There are too many people to ask. Just sort out a FAQ.
First, we should only discuss this question in terms of syntax. If we discuss the differences in programming styles between different people, there is no answer to this question. After all, different people have different preferences.
In terms of syntax, in C ++ (only in C ++ ). There are only two differences between class and struct in defining the type:
(1) permissions are inherited by default. If it is not explicitly specified, the inheritance from class is handled by private inheritance, and the inheritance from struct is handled by public inheritance;
(2) default access permissions of members. By default, class members are private and struct members are public by default.
Apart from these two points, class and struct are basically one thing. There is no syntax difference.
It is not because I have learned C and I always think that there is a big difference between struct and class in C ++. The descriptions listed below may be boring, Because struct and class are basically the same thing, needless to say. However, these instructions may help clarify some common misunderstandings about struct and class:
(1) There can be member functions, including constructor, destructor, overload operators, youyuan class, youyuan structure, youyuan function, virtual function, pure virtual function, static functions;
(2) There can be a lot of public/private/protected modifiers in it;
(3) Although this style is no longer promoted, both syntactically can be initialized using braces: A = {1, 2, 3 }; whether a is a struct or a class, the premise is that the class/structure is simple enough. For example, all members are public and all Members are simple types, no explicitly declared constructor.
(4) A struct can inherit from one class, And a struct can inherit from five classes and five struct at the same time, although this is not good.
(5) If you need to pay attention to the OO principles and style in the design of the class, there is no reason to say that you do not need to pay attention to the design of struct.
(6) Again, all the above statements refer to the C ++ language. As for the situation in C, there is no "class" in C ", c's struct is basically just a syntactic mechanism for packaging data.
---------------------------------------------------------------
Finally, as the two keywords of the language, apart from the above differences in definition types, there is also a little bit: the "class" keyword is also used to define template parameters, just like "typename ". However, the keyword "struct" is not used to define template parameters.