天天看点

朝文分享(46):深入C++(十三)——成员变量

朝文分享(46):深入C++(十三)——成员变量

分享兴趣,传播快乐,增长见闻,留下美好!

亲爱的您,这里是LearningYard新学苑。

今天小编为大家带来“深入C++(十三)——成员变量”,欢迎您的访问。

Share interest, spread happiness, increase knowledge, and leave beautiful.

Dear, this is the LearingYard Academy!

Today, Dive into C++ (13) - Member variables ,welcome to visit!

朝文分享(46):深入C++(十三)——成员变量

一、类对象作类成员

1. Class objects are class members

类对象作为类成员。例如一个学生类有一部手机,而这个手机就是手机类的对象。

Class objects serve as class members. For example, a student class has a phone, and that phone is an object of the phone class.

手机有手机品牌,还需要一个给手机品牌赋值的构造函数。

Phones have a brand, and we need a constructor that assigns a value to the brand.

朝文分享(46):深入C++(十三)——成员变量

学生有姓名和使用的手机,而这里的手机是以手机类的对象来作为学生类的成员变量。创建学生对象,要对姓名和手机品牌赋值。

The student has a name and a cell phone, and the cell phone is the object of the cell phone class as a member variable of the student class. To create a student object, assign values to your name and phone brand.

朝文分享(46):深入C++(十三)——成员变量

创建学生张三,使用的手机为小米。输出正确,说明这个学生对象已经创建成功。

Create student Zhang SAN, the use of mobile phone for Xiaomi. The correct output indicates that the student object was created successfully.

朝文分享(46):深入C++(十三)——成员变量

二、静态成员

2. Static members

静态成员,即被static修饰的成员变量。该成员变量属于整个类,该类的所有对象共享数据。比如井里的水就可以作为静态成员变量。

A static member is a member variable that is static. The member variable belongs to the entire class, and all objects of that class share data. For example, the water in a well can be used as a static member variable.

对于静态成员来说,类内声明,类外初始化。初始化不需要static修饰,但要标明使哪个类下的静态成员。如村有井,井里有100升水。井水初始化时,在前面加上了Village。表明井水时该类下的静态成员。

For static members, they are declared inside the class and initialized outside the class. The initialization does not need static, but it does indicate which class to make a static member of. If the village has a well, there are 100 liters of water in the well. When we initialize the well, we add Village in front of it. Indicates the static member under this class when well water is used.

朝文分享(46):深入C++(十三)——成员变量

村1和村2空用一口井。无论是村1打水还是村2打水,都会使这口井的水减少。

Village 1 and Village 2 use a single well. Whether village 1 draws water or village 2 draws water, it will reduce the water of this well.

朝文分享(46):深入C++(十三)——成员变量

所以无论是村1还是村2,井水都是80升。同样,通过整个类的井水也是80升。

So whether it's village 1 or village 2, the well water is 80 liters. Similarly, the well water that passes through the entire class is also 80 liters.

朝文分享(46):深入C++(十三)——成员变量

三、成员的存储

3. Storage of members

不同成员变量的存储不同。普通成员变量和静态成员变量分开存储。

Member variables are stored differently. Regular and static member variables are stored separately.

一个空类的对象,占1个字节,而不是0个。原因是为了在存储空间上与其他对象分开,避免多个对象存放在同一存储空间。

An object of an empty class that occupies one byte instead of zero. The reason is to keep the storage space separate from other objects and avoid having multiple objects in the same storage space.

朝文分享(46):深入C++(十三)——成员变量

类中存在普通的成员变量,如整型、字符串型。在创建该类的对象时,编译器会为该对象中所有的普通成员开辟空间。

Ordinary member variables exist in classes, such as integers and strings. When an object of this class is created, the compiler makes space for all regular members of the object.

朝文分享(46):深入C++(十三)——成员变量

再在类中添加普通成员变量,对象所占空间越多。说明普通成员变量是存放在对象中的。

When you add regular member variables to the class, the object takes up more space. Indicates that regular member variables are stored in an object.

朝文分享(46):深入C++(十三)——成员变量
朝文分享(46):深入C++(十三)——成员变量

但在类中添加静态成员变量,对象所占空间不变。说明静态成员变量不是存放在对象中的。有前面所学得知,静态成员变量应该存放在全局区。

But when a static member variable is added to a class, the object takes up the same space. Indicates that static member variables are not stored in an object. As you learned earlier, static member variables should be stored in the global area.

朝文分享(46):深入C++(十三)——成员变量

今天的分享就到这里了,

如果您对文章有独特的想法,

欢迎给我们留言。

让我们相约明天,

祝您今天过得开心快乐!

That's all for today's sharing.

If you have a unique idea about the article,

please leave us a message,

and let us meet tomorrow.

I wish you a nice day!

翻译:网易有道翻译

本文由LearningYard学苑整理并发出,如有侵权请后台留言沟通.

文案|Dongyang

排版|Dongyang

审核|hong

Learning Yard 新学苑