c ++中new和operator new之间的区别 [英] difference between new and operator new in c++

查看:91
本文介绍了c ++中new和operator new之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





void * p = new [1024]; - 正常新



void * p = operator new [1024]; - 运营商新





上述声明适用于new和operator new。如果是,请说明我在哪里可以使用以及如何在我的项目中申请。













问候,

Ranjith

解决方案

< blockquote>这会帮你.......



引用:

当你说新的(1024)时,你使用的是C ++关键字new,其中
代表新的运算符。新操作员的工作是:

1.分配内存

2.调用构造函数。

(1)st job是使用C ++的内置'operator new'完成的。 (Bad

超载术语!)。 operator new的原型是

void * operator new(size_t);



因此,在第一个例子中,新的运算符期望'type'和

不是大小,因为它还需要在

类型上调用构造函数。因此第一行给出了编译错误。 (1024不是

类型。)



在第二行,您明确地调用'operator new'。用

换句话说,你所做的只是第一步(分配

内存)。 'operator new'需要你提供的size_t

正确(1024),因此代码编译正确。







访问这里...



between-between-operator-new [ ^ ]


Hi All,


void *p = new [1024]; - normal new

void *p = operator new[1024]; - operator new


the above statement is true for new and operator new. If yes ,please explain where can i use and how can i apply in my project.






Regards,
Ranjith

解决方案

this will help you.......

Quote:

when you say new(1024), you are using C++ keyword new, which
represents the new operator. The job of new operator is :
1. to allocate the memory
2. to call the constructor.
The (1)st job is done using C++'s inbuilt 'operator new'. (Bad
overloading of terminologies!). The prototype of "operator new" is
void* operator new (size_t);

Thus, In the first example, the new operator expects the 'type' and
not the size, since it also needs to call the constructor on that
type. Hence the first line gives compilation error. (1024 is not a
type.)

On the second line, you are explicitly calling the 'operator new'. In
other words, what you are doing is only the first step (allocating the
memory). The 'operator new' requires size_t which you have provided
properly (1024), and hence the code compiles properly.




visit here...

difference-between-new-operator-new[^]


这篇关于c ++中new和operator new之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆