当前位置:网站首页>7.new, delete, OOP, this pointer

7.new, delete, OOP, this pointer

2022-06-23 01:40:00 For financial freedom!

malloc and free Become C Library function
new and delete Called operator

malloc Failed to open up memory , Is the return value and nullptr The comparison ; and new Failed to open up memory , It's by throwing bad_alloc Type of exception !

int* q1 = new int[20];
int* q1 = new int20;
The difference between them is , The first one is only responsible for development without initialization , The second one will be initialized to 0

new How many kinds? ?

int* p1 = new int[20];
int* p2 = new (nothrow) int;// Don't throw exceptions 
const int* p3 = new const int(40);// Open up memory for constants 
// location new
int data = 0;
int* p4 = new(&data) int(50);// Assign a value to the specified memory !

OOP What are the four characteristics of language ?
abstract encapsulation / hide Inherit polymorphic

The memory size of an object is related to its members ! It has nothing to do with member functions ! The total size of the member variable is the same as the structure memory alignment calculation ! Class can define countless objects , Each object has its own member variable , But they share a set of membership methods ( function ).

How does a member function know which object it is called by ? After compiler processing , You know the :
 Insert picture description here

原网站

版权声明
本文为[For financial freedom!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220514413977.html