孔乙己:new的五种写法

2024-06-16 22:08
文章标签 写法 五种 new 孔乙己

本文主要是介绍孔乙己:new的五种写法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

孔乙己:new的五种写法

这个是目标类:_INT
拥有一个字面常量构造函数,和一个平凡析构函数
可以从int构造,也可以隐式转换为int
也可以和int比较大小

class _INT
{
private:int value;
public:constexpr explicit _INT(int _Val0 = 0) noexcept:value(_Val0) {}~_INT() {}operator int(){return value;}bool operator ==(int _Right){return value == _Right;}bool operator <(int _Right){return value < _Right;}};

第一种写法:

  • auto _Ptr1 = new _INT(1);
    对应释放方式:delete _Ptr1;

第二种写法:

  • auto _Ptr2 = new (malloc(sizeof(_INT))) _INT(2);
    对应释放方式:free(_Ptr2);

第三种写法:

  • auto _Ptr3 = new (operator new(sizeof(_INT))) _INT(3);
    对应释放方式:
    _Ptr3->~_INT();
    operator delete(_Ptr3);
    或( delete _Ptr3; )

第四种写法:
char memory[0x1000];

  • auto _Ptr4 = new (memory) _INT(4);
    对应释放方式:
    _Ptr4->~_INT();
    memset(_Ptr4, 0, sizeof(_INT));

第五种写法:
allocator<_INT> alloc;

  • auto _Ptr5 = alloc.allocate(1);
  • allocator_traits<decltype(alloc)>::construct(alloc,_Ptr5, 5);
    对应释放方式:
    allocator_traits<decltype(alloc)>::destroy(alloc, _Ptr5);
    alloc.deallocate(_Ptr5, 1);

在这里插入图片描述


#include <iostream>
#include <memory>
#include <list>
#include <vector>
#include <memory>using namespace std;class _INT
{
private:int value;
public:constexpr explicit _INT(int _Val0 = 0) noexcept:value(_Val0) {}~_INT() {}operator int(){return value;}bool operator ==(int _Right){return value == _Right;}bool operator <(int _Right){return value < _Right;}};int main()
{char memory[0x1000];allocator<_INT> alloc;auto _Ptr1 = new _INT(1);auto _Ptr2 = new (malloc(sizeof(_INT))) _INT(2);auto _Ptr3 = new (operator new(sizeof(_INT))) _INT(3);auto _Ptr4 = new (memory) _INT(4);auto _Ptr5 = alloc.allocate(1);allocator_traits<decltype(alloc)>::construct(alloc,_Ptr5, 5);cout << *_Ptr1 << endl;cout << *_Ptr2 << endl;cout << *_Ptr3 << endl;cout << *_Ptr4 << endl;cout << *_Ptr5 << endl;delete _Ptr1;free(_Ptr2);_Ptr3->~_INT();operator delete(_Ptr3); // delete _Ptr3memset(_Ptr4, 0, sizeof(_INT));allocator_traits<decltype(alloc)>::destroy(alloc, _Ptr5);alloc.deallocate(_Ptr5, 1);return 0;
}

这篇关于孔乙己:new的五种写法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1067680

相关文章

Python实现阶乘的四种写法

《Python实现阶乘的四种写法》本文主要介绍了Python实现阶乘的六种写法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录第一种:推导式+循环遍历列表内每个元素相乘第二种:调用functools模块reduce的php累计

Golan中 new() 、 make() 和简短声明符的区别和使用

《Golan中new()、make()和简短声明符的区别和使用》Go语言中的new()、make()和简短声明符的区别和使用,new()用于分配内存并返回指针,make()用于初始化切片、映射... 详细介绍golang的new() 、 make() 和简短声明符的区别和使用。文章目录 `new()`

MySQL中删除重复数据SQL的三种写法

《MySQL中删除重复数据SQL的三种写法》:本文主要介绍MySQL中删除重复数据SQL的三种写法,文中通过代码示例讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下... 目录方法一:使用 left join + 子查询删除重复数据(推荐)方法二:创建临时表(需分多步执行,逻辑清晰,但会

透彻!驯服大型语言模型(LLMs)的五种方法,及具体方法选择思路

引言 随着时间的发展,大型语言模型不再停留在演示阶段而是逐步面向生产系统的应用,随着人们期望的不断增加,目标也发生了巨大的变化。在短短的几个月的时间里,人们对大模型的认识已经从对其zero-shot能力感到惊讶,转变为考虑改进模型质量、提高模型可用性。 「大语言模型(LLMs)其实就是利用高容量的模型架构(例如Transformer)对海量的、多种多样的数据分布进行建模得到,它包含了大量的先验

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

java线程深度解析(一)——java new 接口?匿名内部类给你答案

http://blog.csdn.net/daybreak1209/article/details/51305477 一、内部类 1、内部类初识 一般,一个类里主要包含类的方法和属性,但在Java中还提出在类中继续定义类(内部类)的概念。 内部类的定义:类的内部定义类 先来看一个实例 [html]  view plain copy pu

string字符会调用new分配堆内存吗

gcc的string默认大小是32个字节,字符串小于等于15直接保存在栈上,超过之后才会使用new分配。

List list = new ArrayList();和ArrayList list=new ArrayList();的区别?

List是一个接口,而ArrayList 是一个类。 ArrayList 继承并实现了List。 List list = new ArrayList();这句创建了一个ArrayList的对象后把上溯到了List。此时它是一个List对象了,有些ArrayList有但是List没有的属性和方法,它就不能再用了。而ArrayList list=new ArrayList();创建一对象则保留了A

vue原理分析(六)--研究new Vue()

今天我们来分析使用new Vue() 之前研究时,只是说是在创建一个实例。并没有深入进行研究 在vue的源码中找下Vue的构造函数 function Vue(options) {if (!(this instanceof Vue)) {warn$2('Vue is a constructor and should be called with the `new` keyword');}thi

GTK中创建线程函数g_thread_new和g_thread_create的区别

使用GThread函数,需要引用glib.h头文件。 这两个接口的核心区别就是  g_thread_create 是旧的接口,现在已经不使用了,而g_thread_new是新的接口,建议使用。 g_thread_create: g_thread_create has been deprecated since version 2.32 and should not be used in n