In C ++, there are only two main differences between class and struct:
- The default permission is inherited. By default, the class is inherited by private, while the struct is inherited by public.
- The default access permission of the member. By default, class members are private and struct members are public by default.
Other features, such as struct and class, are basically the same:
01 |
// An uncommon example. You can also compile struct directly into a class. |
02 |
// The compiling environment is GCC 4.4.1. |
09 |
private: // Access permission Modifier |
12 |
bar(){}; // No parameter Constructor |
13 |
bar(int a){ y = a;}// Constructor with Parameters |
14 |
~bar(); // Fictitious Function |
16 |
virtual void func1() = 0; // Pure virtual function |
19 |
structfoo: protected bar // Inherit |
25 |
void say(string msg) {cout<<msg<<endl;} |
26 |
virtual int func2();// Virtual functions |
You can see:
- All can have member functions: struct can contain the same constructor, destructor, overload operators, youyuan class, youyuan structure, youyuan function, virtual function, pure virtual functions, static functions;
- Although the default access permissions are different, they can all have public/private/protected modifiers;
- Can carry out complex inheritance and multi-inheritance, a struct can inherit from one or more classes, and vice versa.
- Note that it is different from the C language. struct in C is essentially a syntax mechanism for packaging data.
The Google C ++ programming style guide also states:
Use struct only when the operator has data, and use class for others.
In C ++, the keywords struct and class have almost the same meaning. We add semantics to them so that we can reasonably choose which keyword to use for the defined data type.
Struct is used to include only passive objects (passive objects). It may include associated constants, but does not provide function functions outside the data member, the access function is implemented through direct access without calling a method. The method mentioned here is only used for data members, such as constructors, destructor, initialize (), reset (), validate ().
If more function functions are required, the class is more suitable. If you are not sure, use the class directly.
Without STL combination, you can use struct instead of class for functions and features.
Although some C ++ experts claim that they can no longer use the struct keyword, they can always use class {public:} instead. But in fact, struct is still widely used in code. Developers often use struct (partly due to the impact of the C language) to indicate a lightweight record that does not require strict encapsulation. For example, struct is often used to declare a record written to a file or database table structure, while class is mainly used for object-oriented programming.
In general, the main reason struct structures must still be used is:
- Develop and maintain legacy systems.
- Communication with traditional APIS is required.
Of course, sometimes using struct can make the Code look more concise:
1 |
struct Compare { bool operator() { ... } }; |
2 |
std::sort(collection.begin(), collection.end(), Compare()); |
There are too many details about the advanced features and syntax in C ++, so it is necessary to follow certain programming specifications.