C + + in the struct the C in the struct has been expanded, it is no longer just a data structure that contains a different datatype, and it has acquired too much functionality.
can a struct contain member functions? you can!
can a struct inherit? can!!
can a struct achieve polymorphism? Can!!!
??
since all this can be achieved, it and class What difference does it make?
One of the most essential differences is the default access control, which is reflected in two areas:
??
default inherited access rights 。 struct is public class is private
You can write the following code:
struct a
{
char a ;
}
struct B:A
{
Char B ;
}
this time B is a Public Inheritance A the.
if all of the above struct Change into class , then B is a Private Inheritance A the. This is the default inherited access permission.
??
So when we write class inheritance in peacetime, we usually write this:
struct b:public? A
just to indicate that it is Public inheritance, instead of using the default Private inheritance.
??
of course, The default is public inherit or private Inherit, depending on the subclass rather than the base class.
I mean, struct can inherit class , the same class can also inherit struct , then the default inherited access is to see what the subclass is for. struct or is class . as follows:
??
struct a{} ; class b:a{};//private Inheritance
struct c:b{}; //public inheritance
??
2 ) struct as the implementation body of the data structure, its default access control is Public of, and class as the object's implementation body, its default member variable access control is Private the.
??
Note the words above me. , I still emphasize struct class struct class
in fact, in the end is to use struct or is class , completely look at your personal preferences, you can put all of your programs class Replace All struct , it can still run normally. But the best advice I'll give you is: When you think you're going to do something more like a data structure, use a struct, if you want to do something more like an object, then use class .
??
Of course, what I would like to emphasize here is that for access control, it should be clearly stated in the program, rather than relying on the default, which is a good habit and makes your code more readable.
??
speaking of which, a lot of people who know it may think that this topic is over because they know struct and the class of the " only " The difference is access control. It is true that this distinction is only mentioned in many literatures.
??
but it doesn't work on me ."only", but to say that"the most essential", that's because they do have another difference, although that difference is something we might rarely involve. That is : "Class" This keyword is also used to define template parameters, just like "TypeName". but the key word "struct" not used to define template parameters. This is illustrated in the Inside the C + + Object Model written by Stanley B.lippman .
??
The question is discussed here, and basically it should be over. But someone once said that he has found other " differences ",so let's see if this is another difference. As mentioned above,the struct in C+ + is a struct , since it is an extension, it is compatible with all the attributes that the struct in the past C should have. For example you can write:
??
struct A// Define a struct
{
Char C1;
int n2;
Double db3;
};
A a={' P ', 7,3.1415926}; assigning values directly when defined
??
Which means struct can be used at the time of definition {} assigns an initial value. So the question is, isclass OK? change the above struct to classand try it. Error! Oh ~ so the man jumps out and he finds a difference. Let's take a closer look, is this really another difference?
??
you try to struct
Yes, struct also cannot be used {}
Indeed, to {} to assign the initial value, just use an initialization list to initialize the data sequentially, as above if written as a a={' P ', 7}; the c1,n2 is initialized, and db3 No, No. such a simple Copy operations can only occur on simple data structures and should not be placed on objects. Adding a constructor or a virtual function makes the struct More of an object's nature, and makes the {} operation no longer valid.
??
in fact, because of the addition of such functions, the internal structure of the class has changed. And adding a normal member function? You will find that {} is still available. In fact, you can think of ordinary functions as an algorithm of data structure, which does not break the characteristics of its data structure.
??
well, seeing here, we find that even struct want to use {} to assign an initial value, it must also meet a number of constraints, which in effect allow a struct to embody a data mechanism rather than a class's characteristics.
??
Then why are we just going to struct changed to class {} can not be used?
Actually, the problem happens to be the -- access Control! You see, what have we forgotten? Yes, struct changed to class public private {} to assign the initial value. Add a public class is also available {} Span style= "font-family: Song Body", and struct No difference!!!
??
To make a summary, from the above differences, we can see that struct more suitable as a data structure of the implementation of the body, class more suitable as an object's implementation body.
The difference between struct and class in C + +