普通new和placement new的重载(转)

原文:https://www.cnblogs.com/wanmeishenghuo/p/9651363.html

 

参考:

https://www.jb51.net/article/41331.htm

https://blog.csdn.net/windgs_yf/article/details/81146817

https://blog.csdn.net/songchuwang1868/article/details/81353577?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

https://blog.csdn.net/aishuirenjia/article/details/102979457?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

 

https://www.cnblogs.com/c-slmax/p/5948662.html

https://blog.csdn.net/High_High/article/details/7397268?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-7.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-7.nonecase

https://blog.csdn.net/lc_910927/article/details/29829811

https://blog.csdn.net/zhangxinrun/article/details/5940019

https://blog.csdn.net/linuxheik/article/details/80449059

https://www.cnblogs.com/zhoug2020/archive/2012/04/06/2434245.html

https://blog.csdn.net/qq_26822029/article/details/81213537

 

在全局重“定义”operator new。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cstdlib>
 
using namespace std;
 
class Test
{
public:
    Test()
    {
        cout << "Test()" << endl;
    }
 
  
 
 
};
 
   void* operator new(unsigned int size)
    {
        void* ret = malloc(sizeof(int) * size);
 
        cout << "normal new" << endl;
 
        return ret;
    }
     
int main()
{
    Test* t = new Test();
 
    Test t2;
 
 
    return 0;
}

 结果如下:

 

 

在全局重载placement new

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cstdlib>
 
using namespace std;
 
class Test
{
public:
    Test()
    {
        cout << "Test()" << endl;
    }
 
 
 
 
};
 
 void* operator new(unsigned int size, void* loc)
    {
        cout << "placement new" << endl;
        return loc;
    }
 
int main()
{
    char mem[100];
    mem[0] = 'A';
    mem[1] = '\0';
    mem[2] = '\0';
    mem[3] = '\0';
 
   Test* t1 = new((void*)mem)Test();
 
   
 
 
    return 0;
}  

结果如下:

 

 可以看出,不能在全局重“定义”placement new,参考https://blog.csdn.net/windgs_yf/article/details/81146817

 

对于自定义对象,我们可以重载普通new操作符,这时候使用new Test()时就会调用到我们重载的普通new操作符。

示例程序:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cstdlib>
 
using namespace std;
 
class Test
{
public:
    Test()
    {
        cout << "Test()" << endl;
    }
 
    void* operator new(unsigned int size)
    {
        void* ret = malloc(sizeof(int) * size);
 
        cout << "normal new" << endl;
 
        return ret;
    }
 
 
};
 
 
int main()
{
    Test* t = new Test();
 
    Test t2;
 
 
    return 0;
}

执行结果如下:

 

调用placement new,程序如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cstdlib>
 
using namespace std;
 
class Test
{
public:
    Test()
    {
        cout << "Test()" << endl;
    }
 
    void* operator new(unsigned int size)
    {
        void* ret = malloc(sizeof(int) * size);
 
        cout << "normal new" << endl;
 
        return ret;
    }
 
 
};
 
 
int main()
{
    Test* t = new Test();
 
    Test* t1 = new((void*)t)Test();
 
    Test t2;
 
 
    return 0;
}

编译结果如下:

提示我们没有对应的函数,这是因为在类test中定义了operator new,  全局作用域中的new函数的实现就被隐藏了,重载都必须在同一作用域,所以在类test这个作用域中,没有实现placement new。

若此时,删除类中的operator new的定义,也是可以编译通过的,默认使用了全局的operator new和placement new。

在这里,我们在类中加入placement new,更改程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <cstdlib>
 
using namespace std;
 
class Test
{
public:
    Test()
    {
        cout << "Test()" << endl;
    }
 
    void* operator new(unsigned int size)
    {
        void* ret = malloc(sizeof(int) * size);
 
        cout << "normal new" << endl;
 
        return ret;
    }
 
    void* operator new(unsigned int size, void* loc)
    {
        cout << "placement new" << endl;
        return loc;
    }
 
};
 
 
int main()
{
    Test* t = new Test();
 
    Test* t1 = new((void*)t)Test();
 
    Test t2;
 
 
    return 0;
}

结果如下:

 

 再次给出一个测试程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cstdlib>
 
using namespace std;
 
class Test
{
public:
    Test()
    {
        cout << "Test()" << endl;
    }
 
    void* operator new(unsigned int size)
    {
        void* ret = malloc(sizeof(int) * size);
 
        cout << "normal new" << endl;
 
        return ret;
    }
 
    void* operator new(unsigned int size, void* loc)
    {
        cout << "placement new" << endl;
        return loc;
    }
 
};
 
 
int main()
{
    Test* t = new Test();
 
    Test* t1 = new((void*)t)Test();
 
    Test t2;
 
    Test* t3 = new((void*)&t2)Test();
 
    int a = 3;
 
    int* p = new((void*)&a)int(10);
 
    cout << "a = " << a << endl;
 
    return 0;
}

 

运行结果如下:

可以看到普通内置类型可以直接使用placement new。

 

总结:

1、在全局中可以可重复定义operator new;

2、在全局中不能重复定义placement new;

3、一旦在类中定义了operator new或placement new,全局中的所有new函数将被隐藏(函数的重载必须发生在同一作用域)

 

 

 

  

  

  

posted on   lh03061238  阅读(231)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示