当前位置:网站首页>In insect classes and objects

In insect classes and objects

2022-06-26 13:48:00 Hua Weiyun

Scope of class

== Class defines a new scope ==, All members of a class are in the scope of the class . Define members outside the class , Need to use ==:: Scope resolver == Indicate which class domain the member belongs to

Class instantiation

The process of creating objects with class types , Called class instantiation

  1. == Class is just a model == The same thing , Defines which members of the class , Define a class == No actual memory space allocated == To store it
  2. A class can instantiate multiple objects ,== Instantiated object Take up actual physical space , Store class member variables ==
  3. Let's make an analogy .== Class to instantiate an object is like building a house out of a building plan , Class is like a blueprint ==, Just design what you need , But there are no physical buildings , The same class is just a design , Only the instantiated object can actually store data , Take up physical space

image-20220113131224042

How class objects are stored

Object contains the members of the class

image-20220113120026304

defects : The member variables in each object are different , But call the same function , If stored in this way , When a class creates multiple objects ,== A copy of the code is saved in each object , Save the same code multiple times , Waste space . So what's the solution ?==

Save only member variables , Member functions are stored in public code snippets

image-20220113120313319

image-20220113130116451

== The size of a class , In fact, it is in this class ” Member variables ” The sum of the , Of course, memory alignment is also necessary , Notice the size of the empty class , Empty classes are special , The compiler gives an empty class a byte to uniquely identify the class .==

Structure memory alignment rules

  1. The first member is offset from the structure by 0 The address of .
  2. Other member variables are aligned to a number ( Align numbers ) An integral multiple of the address of . == Be careful : Align numbers = Compiler default alignment number And The smaller value of the member size .==
    VS The default alignment number in is 8
  3. The total size of the structure is :== Maximum number of alignments ( The maximum of all variable types and the minimum of default alignment parameters ) Integer multiple .==
  4. If the structure is nested , The nested structure is aligned to an integral multiple of its maximum alignment , The overall size of the structure is
    All maximum alignments ( The number of alignments with nested structures ) Integer multiple .

image-20220113133118709

this The pointer

this Pointer leading out

image-20220115213043584

image-20220115215052964

At this point, someone should say that they are accessing the same function , So how to visit their respective dates

this The properties of the pointer

  1. this The type of pointer : Class types * const
  2. Only in “ Member functions ” Internal use of
  3. ==this Pointer is essentially a parameter of a member function ==, When an object calls a member function , Pass the object address as an argument to this Shape parameter . therefore == Objects are not stored this The pointer ==.
  4. ==this Pointer is the first implied pointer parameter of a member function , Generally, the compiler passes through ecx Register auto transfer , No need for user delivery ==

image-20220115223353105

【 Interview questions 】

==this Where does the pointer exist ?==

this The pointer is stored in the stack , because this It's a parameter , Parameters , Local variables are in the stack . But different compilers have some differences , Even different versions of the same compiler will be a little different , such as vs13 It's using ecx Register stored ,vs19 It's using rcx Register stored

image-20220115231552879

==this Can the pointer be null ?==

Look at a disgusting question before you say this

image-20220116002237009

therefore this Cannot be null pointer , Null pointer compilation does not report errors , Because he is not a grammatical error , The compiler can't check

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261305525099.html