Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the relationship between structures and classes in C++?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what is the relationship between structures and classes in C++, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Let's take a look at it!

1. The relationship between C++ structure and class.

Why do we mention structures when talking about the size of C++ classes? because many students like me may have come into contact with structure alignment in C language, but do not understand how to calculate the size of the class, in which the concept of memory alignment is used. For the concept of memory alignment has been said before, you can refer to this blog: C++ structure byte alignment and common body size understand the memory size of the structure You need to understand the relationship between classes and structures:

In C++, the only difference between structures and classes is that structures and classes have different default access control properties.

Class, for members that do not specify an access control property, the access control property is a private type (private).

Structure, for members that do not specify any access control properties, their access control properties are public types (public).

So the size of the structure and the size of the class is the same, in C++, structures can also achieve inheritance and polymorphism (may be surprised, you can take a look at this blog post: details of the difference between C++ structures and classes, but also to be clear, the space occupied by static member variables and member functions is definitely not the space of structures or classes. Also, the size of the empty class is 1 byte to make it easier for the class to instantiate.

The code validates:

# include using namespace std;struct Node {}; class Test {}; int main () {cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report