The difference between struct and class

    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.

    in C++

  • struct can contain member functions
  • struct can inherit
  • struct can achieve polymorphism

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.

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:

python 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继承

struct C : B{}; //public继承

2) struct as the implementation of the data structure, its default data access control is public, and class as the implementation of the object, 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 we may rarely touch on that difference. 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 Stanley B.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 //定义一个struct
{
char c1;
int n2;
double db3;
};
A a={'p',7,3.1415926}; //定义时直接赋值

    That is to say, struct can be initialized with {} when it is defined. 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 sequentially, as above, if it is written as A a={'p',7}; then c1 and n2 are initialized, and db3 no. 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! ! !

make a summary

From the above difference, we can see that struct is more suitable to be regarded as the realization body of a data structure, and class is more suitable to be regarded as the realization body 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.
Reference blog:
The difference between value types and reference types, the difference between struct and class

Guess you like

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