Turn from 30734139
At first, let's talk about one of the most basic, but also the most easily overlooked question of what is the difference between a struct and a class in--c++?
If you talk about the difference between a struct in C and a class in C + +, you should tell me a lot. But I'm talking about a struct in C + +, will you still tell me that? Do you think the struct in C is the same as the struct in C + +?
Asked by me, perhaps you would squeak and say: "Not the same." Indeed, it is not the same, so where is the difference?
In fact, the two languages C and C + +, in addition to the grammatical similarities, the idea is completely different. The original idea of C + + is to expand C-"Abetterc", but in fact, such "expansion" can no longer be called expansion, I would prefer to think of C + + as a new language, not just expansion. Or maybe, c + + and c The biggest relationship, just their name, if C + + is not called C + +, and called d++, you may not think of their relationship so closely. Of course, these words are only to ridicule, C + + is indeed on the basis of C development up.
I mention the idea of different, the key is Oo, this idea of the whole software programming impact is too large, so I would say that C + + is more like a new language. The
has said so much nonsense, we still go back to the question we discuss. The struct in the
C + + expands the struct in C, which 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? Yes! Can the
struct inherit? Yes!! Can the
struct implement polymorphism? Yes!!!
There are a lot of people who should already know the truth, but there are always people who don't know, and it's amazing to see them. Yes, I was equally surprised when I first noticed the fact.
Since all of this can be achieved, what difference does it make with class? One of the most essential differences in
is the default access control, which is reflected in two areas:
1) The default inherited access rights. The struct is public and the class is private. If you do not know what is public inheritance, what is private inheritance, you can go to search books, here is not discussed. You can write the following code: struct A{char A;} ; struct B:a{char B;} At this time, B is the public inherits a. If all of the above struct is changed to class, then B is private to inherit a. This is the default inherited access permission. So when we write class inheritance, we usually write this: struct b:public A is to indicate that it is a public inheritance, not a default private inheritance. Of course, the default is public or private inheritance, depending on the subclass rather than the base class. I mean, the struct can inherit class, the same class can inherit the struct, then the default inherited access permission is to see whether the subclass is the struct or class. As follows: struct A{};class b:a{};//private inherits struct c:b{};//public inherits 2) struct as the implementation of the data structure, its default access control is public, and class as the object of the implementation of the body, Its default member variable access control is private.
Pay attention to my words above, I still emphasize that struct is a data structure of the implementation of the body, although it can be used as a class. I still call the variables in the struct as data, variables within class are called members, although they are no different. In fact, in the end is a struct or class, completely look at the personal preferences, you can all the classes in your program are all replaced with a struct, it can still be very normal operation. 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, many people who know it may think that this topic is over because they know that the "only" difference between struct and class is access control. It is true that this distinction is only mentioned in many literatures.
But I do not use the "only", but the "most essential", that is because, they do have another difference, although that difference we can usually be very little involved. That is: the keyword "class" is also used to define template parameters, like "TypeName". However, the keyword "struct" is not used to define template parameters. This is illustrated by the inside the C + + Object model written in Stanleyb.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 an extension of the struct in C, and since it is an extension, it will be compatible with all the attributes that the struct in the past C should have. For example you can write:
struct a//defines a struct
{
Char C1;
int n2;
Double db3;
};
A a={' P ', 7,3.1415926}; Assigning values directly when defined
This means that the struct can use {} to assign an initial value at the time of definition. So the question is, is class OK? Change the above struct to class and 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?
What would you find if you tried to add a constructor (or virtual function) to the above struct? Yes, structs cannot use {} to assign an initial value. Indeed, the initial value is assigned in the form of {}, with an initialization list that initializes the data sequentially, as above if it is written as aa={' P ', 7}, then C1,N2 is initialized, and DB3 does not. Such a simple copy operation can only occur on a simple data structure, not on an object. 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. As for the difference between the virtual function and the ordinary member function, I will write a specific article to discuss. So, as we see here, we find that even if a struct wants to use {} to assign an initial value, it must satisfy many constraints, which in effect make the struct more of an attribute of a data organization than of a class. So why can't we just change the struct to class,{} on top of it? In fact, the problem happens to be what we said before-access control! You see, what have we forgotten? Yes, when the struct is changed to class, access control is changed from public to private, which of course cannot be assigned the initial value with {}. Add a public, you will find that class can also be used {}, and struct no difference!!! To make a summary, from the above differences, we can see that the struct is more suitable as a data structure of the implementation of the body, class more suitable as an object of the implementation of the body. So I'm going to ask when to use a struct when to use class advice. If you have a different view, welcome to the discussion
The struct of C + + learning