当前位置:网站首页>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(){}};边栏推荐
- [Playwright Test Tutorial] 5 minutes to get started
- MRS: Introduction to the use of Alluxio
- KingbaseES数据库启动失败,报“内存段超过可用内存”
- 自定义通用分页标签02
- SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri
- 基于Qt的目录统计QDirStat
- 嵌入式数据库开发编程MySQL(全)
- 2022支付宝C2C现金红包PHP源码DEMO/兼容苹果/安卓浏览器和扫码形式
- 【项目实现】Boost搜索引擎
- new Date将字符串转化成日期格式 兼容IE,ie8如何通过new Date将字符串转化成日期格式,js中如何进行字符串替换, replace() 方法详解
猜你喜欢

数组相关 内容 解析

LeetCode每日一题(2285. Maximum Total Importance of Roads)

Homemade bluetooth mobile app to control stm8/stm32/C51 onboard LED

小程序+新零售,玩转行业新玩法!

new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail

数据安全峰会2022 | 美创DSM获颁“数据安全产品能力验证计划”评测证书

十一种概率分布

STM8S105k4t6c---------------Light up LED

基于 SSE 实现服务端消息主动推送解决方案

Implementing a server-side message active push solution based on SSE
随机推荐
2千兆光+6千兆电导轨式网管型工业级以太网交换机支持X-Ring冗余环网一键环网交换机
STM8S105k4t6c---------------Light up LED
Mockito单元测试
sqoop ETL tool
SQL injection in #, - +, - % 20, % 23 is what mean?
Oracle与Postgresql在PLSQL内事务回滚的重大差异
4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
出现504怎么办?由于服务器更新导致的博客报504错误[详细记录]
Exclude_reserved_words 排除关键字
Brush esp8266-01 s firmware steps
SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri
Y86. Chapter iv Prometheus giant monitoring system and the actual combat, Prometheus storage (17)
异步编程解决方案 Generator生成器函数、iterator迭代器、async/await、Promise
Pine Script | How to display and typeset a plot switch?
C language -- ring buffer
【源码】使用深度学习训练一个游戏
什么是数字孪生智慧城市应用场景
怎样提高网络数据安全性
网络工程师入门必懂华为认证体系,附系统学习路线分享
Mini program + new retail, play the new way of playing in the industry!