The difference between struct and class (reproduced)

Source: Internet
Author: User

Respect original, reprint Source: http://blog.sina.com.cn/s/blog_48f587a80100k630.html

The struct in C + + has been augmented by the struct in a, which is no longer just a data structure that contains a different datatype, and has acquired too much functionality.
Can a struct contain member functions? Yes!
Can a struct inherit? Yes!!
Can a struct achieve polymorphism? Yes!!!

Since all this can be achieved, what difference does it make with class?

One of the most essential differences is the default access control:

The struct is public and the class is private.
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 in peacetime, we usually write this:
Class 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, thestruct 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 inheritance
struct c:b{};//public inheritance

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

I still emphasize that struct is an implementation of a data structure, 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 the end is a struct or class, completely look at the personal preferences, you can all the class in the program is replaced by a 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, 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: thekeyword "class" is also used to define template parameters, like "TypeName". However, the keyword "struct" is 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 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//define 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 be assigned an initial value with {}.
It is true that 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 a a={' 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.

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.

The difference between struct and class (reproduced)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.