Struct for C++ Learning

 Reprinted from https://blog.csdn.net/bestconvenient/article/details/30734139

At the very beginning, let's discuss one of the most basic and most easily overlooked questions - what is the difference between struct and 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 structs in C++, will you still tell me that? Would you think that a struct in C is the same as a struct in C++?
When asked by me like this, maybe you will squeak and say: it's not the same. Indeed, it is not the same, so what is the difference?
In fact, the concepts of C and C++ are completely different except for the similarities in syntax. The original idea of ​​C++ is to expand C - "abetterc", but in fact, such "expansion" can no longer be called expansion, I prefer to regard C++ as a new language, not just expansion. Or maybe, the biggest relationship between C++ and C is their names. If C++ is not called C++, but D++, you may not think about the relationship between them so closely. Of course, these words are just a joke, C++ is indeed developed on the basis of C.
The reason why I mentioned that the concept is different is that the key point is oo. This idea has too much impact on the entire software programming, so I would say that C++ is more like a new language.
Having said so much nonsense, let's go back to the issues we discussed.
The struct in C++ extends the struct in C, it is no longer just a data structure containing different data types, it has acquired too many functions.
Can a struct contain member functions? can!
Can struct inherit? can! !
Can struct achieve polymorphism? can! ! !
There are many people who should already know this fact, but there are always people who don't and will be surprised to see it. Yes, I was equally astonished when I first noticed this fact.
Since it can achieve these, what is the difference between it and class?
The most essential difference is the default access control, which is reflected in two aspects:

 
1) Default inherited access rights. struct is public, class is private.
If you don't know what is public inheritance and what is private inheritance, you can go to the book, which will not be discussed here.
You can write code like this:
struct A
{
char a;
};
struct B : A
{
char b;
};
At this time, B is public inheriting A. If the above struct is changed to class, then B inherits A privately. This is the default inherited access. So when we usually write class inheritance, we usually write like this:
struct B : public A
It is to indicate that it is public inheritance, rather than the default private inheritance.
Of course, whether the default is public inheritance or private inheritance depends on the subclass rather than the base class. I mean, a struct can inherit a class, and a class can also inherit a struct, so the default inheritance access authority depends on whether the subclass uses a struct or a class. as follows:
struct A{};
class B : A{};//private inheritance
struct C : B{}; //public inheritance
 
2) struct is the implementation of the data structure, and its default data access control is public, while class is the implementation of the object, and its default member variable access control is private.
Note my wording above, I still emphasize that struct is an implementation of a data structure, although it can be used like a class. I still call variables in struct data and variables in class members, although there is no difference between them. In fact, whether to use struct or class depends entirely on personal preference. You can replace all classes in your program with struct, and it can still run normally. But the best advice I can give is: when you think what you're going to do is more of a data structure, use struct, and if what you're doing is more of an object, use class.
Of course, what I want to emphasize here is that for access control, it should be clearly pointed out in the program instead of relying on the default. This is a good habit and makes your code more readable.

At this point, many people who know may think that this topic can be closed, because they know that the "only" difference between struct and class is access control. It is true that only this difference is mentioned in many literatures.
But I didn't use "the only" above, but said "the most essential", that's because they do have another difference, although that difference we may rarely touch on. That is: the "class" keyword is also used to define template parameters, just like "typename". But the keyword "struct" is not used to define template parameters. This is explained in Inside the C++ Object Model by StanleyB.Lippman.
The discussion of the problem is here, and it should basically be over. But someone has said that he has found other "differences", so let's see if this is another difference.
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 in C in the past should have. For example, you can write:
struct A//Define a struct
{
char c1;
int n2;
double db3;
};
A a={'p',
7,3.1415926 When defining, use {} to assign an initial value. So the question is, does class work? Change the above struct to class and try it out. Error! Oh~ So the man jumped out and said, he found another difference. Let's take a closer look, is this really another difference?
You try to add a constructor (or virtual function) to the above struct, what do you find? Yes, struct can't use {} to assign initial value. Indeed, assigning the initial value in the way of {} just uses an initialization list to initialize the data in order, as above, if it is written as Aa={'p',7}; then c1 and n2 are initialized, while db3 does not . Such a simple copy operation can only occur on simple data structures, not on objects. Adding a constructor or a virtual function will make the struct more reflect the characteristics of an object, so that the {} operation is no longer valid. In fact, adding such a function changes the internal structure of the class. What about adding a normal member function? You will find that {} is still available. In fact, you can understand an ordinary function as an algorithm for a data structure, which does not break the characteristics of its data structure. As for the difference between virtual functions and ordinary member functions, I will write an article to discuss them in detail.
So, seeing this, we find that even if struct wants to use {} to assign an initial value, it must meet a lot of constraints. These conditions actually make struct more of a data organization rather than a class. So why do we just change struct to class above, and {} can't be used? In fact, the problem happens to be what we talked about before - access control! Look, what have we forgotten? Yes, when the struct is changed to a class, the access control is changed from public to private, so of course, {} cannot be used to assign the initial value. Add a public, and you will find that class can also use {}, which is no different from struct! ! !
To sum up, from the above differences, we can see that struct is more suitable as the realization of a data structure, and class is more suitable as the realization of an object. So I will give advice on when to use struct and when to use class. If you have different views, welcome to discuss

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326085126&siteId=291194637