当前位置:网站首页>How class only static allocation and dynamic allocation
How class only static allocation and dynamic allocation
2022-08-04 03:36:00 【Can't beat the little ants】
Concept
- Static allocation
The compiler allocates memory in stack space for objects.Using this method, the constructor of the class is called directly.For example:
A a
- Dynamic build
Allocate memory in heap space.This process is divided into two steps:
[first step] execute the operator new( ) function to allocate memory in the heap space;
[second step] call the class constructor to construct the object.
Static allocation
Ideas
- To restrict the new operator to implement class objects can only be created on the stack.This restricts the class object from calling the new operator.
class A {private:void* operator new(size_t t){} //Set as privatevoid operator delete(void* ptr){} //If new is overloaded, delete needs to be overloaded.Corresponds to overloading.public:A(){}~A(){}};
Dynamic allocation
There are a few points to note.
- Only allowing dynamic allocation requires disallowing direct calls to constructors.But the constructor needs to be called indirectly, so the constructor cannot be made private.
- When the compiler allocates stack space for a class object, it will first check the accessibility of the class's destructor (in fact, not only the destructor, but as long as it is a non-static function, the compiler willan examination).If the class's destructor is not accessible outside the class, the compiler refuses to allocate memory for the class object on stack space.This also disables static allocation.Therefore, destructors can be defined as private.
- Because the destructor is defined as private, an additional destructor needs to be defined to release memory.
class A {public:A(){}void destory(){delete this;}private:~A(){}};
边栏推荐
- Hey, I had another fight with HR in the small group!
- 系统太多,多账号互通如何实现?
- Implementing a server-side message active push solution based on SSE
- Metaverse "Drummer" Unity: Crazy expansion, suspense still exists
- 复现20字符短域名绕过
- 数据集类型转换—TFRecords文件
- JVM的内存模型简介
- Polygon zkEVM网络节点
- "Introduction to nlp + actual combat: Chapter 8: Using Pytorch to realize handwritten digit recognition"
- 哎,又跟HR在小群吵了一架!
猜你喜欢
从图文展示到以云为核,第五代验证码独有的策略情报能力
仿牛客论坛项目梳理
Eight guiding principles to help businesses achieve digital transformation success
Functions, recursion and simple dom operations
自定义通用分页标签02
【项目实现】Boost搜索引擎
十一种概率分布
【 observe 】 super fusion: the first mention of "calculate net nine order" evaluation model, build open prosperity of power network
数据安全峰会2022 | 美创DSM获颁“数据安全产品能力验证计划”评测证书
docker+bridge+redis master-slave+sentry mode
随机推荐
Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch
Based on the statistical QDirStat Qt directory
机器学习模型的“可解释性”
Senior PHP development case (1) : use MYSQL statement across the table query cannot export all records of the solution
db2中kettle报错 Field [XXX] is required and couldn‘t be found 解决方法
sql注入一般流程(附例题)
什么是数字孪生智慧城市应用场景
【医保科普】维护医保基金安全,我们可以这样做
Implementing a server-side message active push solution based on SSE
汇编语言之栈
FPGA解析B码----连载3
docker+bridge+redis master-slave+sentry mode
Eight guiding principles to help businesses achieve digital transformation success
Hey, I had another fight with HR in the small group!
Exclude_reserved_words 排除关键字
Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
MySQL 查询练习(1)
RSS订阅微信公众号初探-feed43
打造一份优雅的简历
SQL injection in #, - +, - % 20, % 23 is what mean?