I,
The struct in C ++ expands the struct in C. It is no longer just a data structure that contains different data types. It has obtained too many functions.
Can struct contain member functions? Yes!
Can struct be inherited? Yes !!
Can struct realize polymorphism? Yes !!!
One essential difference is the default access control, which is embodied in two aspects:
1) default inherited access permissions. Struct is public and class is private.
Write the following code:
Copy codeCode: struct
{
Char;
};
Struct B:
{
Char B;
};
In this case, B inherits A from public. If all the above struct values are changed to class, B inherits A from private. This is the default inherited access permission. Therefore, when writing class inheritance, we usually write as follows:
Struct B: public
It is to indicate that it is a public inheritance, rather than a default private inheritance.
Of course, whether it is public or private by default depends on the subclass rather than the base class. I mean, struct can inherit the class, and the class can also inherit the struct, so the default inherited access permission is to see whether the Sub-class is the struct or class. As follows:
Struct {};
Class B: A {}; // private inheritance
Struct C: B {}; // public inheritance
2) struct is the implementation body of the data structure. The default data access control is public, while the class is the implementation body of the object. The default access control of member variables is private.
3) the keyword "class" is also used to define template parameters, just like "typename ". However, the keyword "struct" is not used to define template parameters.
4) As mentioned above, struct in C ++ is an extension of struct in C. Since it is an extension, it must be compatible with all the features that struct should have in the past in C. For example, you can write:
Struct A // define A struct
{
Char c1;
Int n2;
Double db3;
};
A a = {'P', 7, 3.1415926}; // value assigned directly when defining
That is to say, struct can use {} to assign an initial value during definition.
Add a constructor (or virtual function) to the above struct, And the struct cannot use {} to assign the initial value. Indeed, the initial value is assigned in the form of {}, but an initialization list is used to initialize the data in order, as shown in the preceding example if A = {'P', 7 }; then c1 and n2 are initialized, while db3 does not. Such a simple copy operation can only occur in a simple data structure, rather than an object. Adding a constructor or a virtual function will make struct more reflect the characteristics of an object, and this {} operation is no longer valid. In fact, it is because such a function is added that the internal structure of the class has changed. What about adding a common member function? You will find that {} is still available. In fact, you can understand a common function as an algorithm for data structure, which does not break the characteristics of its data structure. I will write an article to discuss the differences between virtual functions and common member functions.
Then, we can see that, even if struct wants to use {} to assign an initial value, it must satisfy many constraints, in fact, these conditions allow struct to better reflect the characteristics of a data organization rather than a class. So why can't we just change struct to class and {} It's useless? In fact, the problem happened to be what we mentioned earlier -- access control! What did we forget? Yes. When struct is changed to class, the access control is changed from public to private, so {} cannot be used to assign the initial value. After adding a public object, you will find that the class can also be used {}, which is no different from struct !!!
From the above differences, we can see that struct is more suitable to be regarded as the implementation body of a data structure, and class is more suitable to be regarded as the implementation body of an object.
II,
About using braces for initialization
Class and struct cannot be initialized using braces if constructors are defined.
If no constructor is defined, struct can be initialized with braces.
If no constructor is defined and all member variables are public, you can use braces to initialize them.
About default access permissions
The default access permission for members in class is private, while that for struct is public.
Inheritance Methods
By default, class inheritance is private inheritance, while struct inheritance is public inheritance by default.
Check the following code (check the error message provided by the compiler ):Copy codeThe Code is as follows: class T1
{
Public:
Void f ()
{
Cout <"T1: f ()" <endl;
}
Int x, y;
};
Struct T2
{
Int x;
Void f () {cout <"T2: f ()" <endl ;}
};
Struct TT1: T1
{
};
Class TT2: T2
{
};
Int main ()
{
TT1 t1;
TT2 t2;
T1.f ();
T2.f ();
}
About templates
In the template, class or typename can be used before the type parameter. If struct is used, the meaning is different. struct is followed by "non-type template parameter ", the class or typename follows the type parameter.
Template <struct X>
Void f (X x)
{
}
// Error message: d: codecpptestcpptestcpptest. cpp (33): error C2065: 'X': undeclared identifier