当前位置:网站首页>Classes and objects -- object model and this pointer
Classes and objects -- object model and this pointer
2022-06-13 09:26:00 【Hezeze】
.1 The object model
Member variables are stored separately from member functions .
Memory occupied by empty objects 1 byte , Only non static member variables belong to objects of class
.2this The pointer
Because multiple objects of the same type will share a piece of code , In order to distinguish between ,C++ Provide this The pointer Point to the object to which the called member function belongs ( There is no need to define direct use )
- When a formal parameter has the same name as a member variable , You can use this Pointer discrimination
class Person {
public:
void test(int age) {
this ->age = age;
}
int age;
};
- When the object itself is returned in a member function in a class , Use return *this


.3 Null pointer to access member function
this Error caused by null pointer , To ensure robustness, you need to verify
class Person {
public:
int age;
void show() {
if (this == NULL) {
return; // testing this Is the pointer null
}
cout << age << endl; // Equivalent to calling this->age
}
};
void test() {
Person* p1 = NULL;
p1->show();
}
.4 const Modifying Member Functions
Constant function : Add... After the member function const
- Member properties cannot be modified in a constant function , Modification is this Point to , Make it not modifiable
- Prefix the member attribute declaration with the keyword mutable after , Can only be modified in a constant function
Regular object :const Class name p;
- Constant objects cannot modify member properties , Unless added mutable
- A constant object can only call a constant function
class Person {
public:
mutable int age ;
//this The essence of pointer is pointer constant , The point of the pointer cannot be modified
void show() const
{
this->age = 10;
}
};
边栏推荐
- ROS2之OpenCV人脸识别foxy~galactic~humble
- LeetCode 201. 数字范围按位与
- How to build an aby framework and run an instance
- LeetCode 322. 零钱兑换
- C language: sanziqi
- Can the operation of the new BMW I3 meet the expectations of the famous products of the 3 series?
- 20211104 why are the traces of similar matrices the same
- LeetCode 72. Edit distance
- JUC atomic reference and ABA problem
- LeetCode 6095. 强密码检验器 II
猜你喜欢

(state compression dp+good) acwing 291 Mondrian's dream

C/S模型与P2P模型

Class loading overview

(bfs) acwing 844. Labyrinth
![1-4 message passing interface [CSP authentication]](/img/db/aecda548693cdfb0e740bcf1a1c823.jpg)
1-4 message passing interface [CSP authentication]

攻防世界PWN play 条件竞争漏洞的利用

Jenkins access openldap user authentication

turtle库显示系统时间

Alibaba senior experts analyze the standard design of protocol

(dfs+ pruning + checkerboard problem +dood) acwing 843 N-queen problem
随机推荐
Necessary and sufficient conditions for diagonalization of 20211115 matrix; The full rank matrix does not necessarily have n linearly independent eigenvectors; Symmetric matrices must be diagonalized
Timestamp to localdate
C language: deep understanding of character functions and string functions (2)
Heap
C language: callback function
QML(06)——qml. Add a new folder under QRC
Can the operation of the new BMW I3 meet the expectations of the famous products of the 3 series?
202012 CCF test questions
LeetCode 6096. Success logarithm of spells and potions (binary search)
Leetcode points to offer 30 Stack containing min function
turtle库显示系统时间
20211028 adjustment and tracking
Online debugging tool Arthas advanced
BGP 联邦+Community
C language: five custom types
Opencv face recognition of ros2 foxy~galactic~humble
Delete soft link
@Value不生效及extend/implement其他类无法注入bean的手动注入
LeetCode 583. Deleting two strings
(dfs+ pruning + checkerboard problem +dood) acwing 843 N-queen problem