C++ “新操作员”和“新操作员”之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1885849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between 'new operator' and 'operator new'?
提问by Sandeep
What is difference between "new operator" and "operator new"?
“new operator”和“operator new”有什么区别?
回答by Jerry Coffin
I usually try to phrase things differently to differentiate between the two a bit better, but it's a good question in any case.
我通常会尝试用不同的措辞来更好地区分两者,但无论如何这是一个好问题。
Operator new is a function that allocates raw memory -- at least conceptually, it's not much different from malloc()
. Though it's fairly unusual unless you're writing something like your own container, you can call operator new directly, like:
运算符 new 是一个分配原始内存的函数——至少在概念上,它与malloc()
. 尽管除非您正在编写类似自己的容器的内容,否则这很不寻常,但您可以直接调用 operator new,例如:
char *x = static_cast<char *>(operator new(100));
It's also possible to overload operator new either globally, or for a specific class. IIRC, the signature is:
也可以全局或为特定类重载 operator new。IIRC,签名是:
void *operator new(size_t);
Of course, if you overload an operator new (either global or for a class), you'll also want/need to overload the matching operator delete as well. For what it's worth, there's also a separate operator new[] that's used to allocate memory for arrays -- but you're almost certainly better off ignoring that whole mess completely.
当然,如果您重载运算符 new(全局或类),您还需要/需要重载匹配的运算符 delete。就其价值而言,还有一个单独的运算符 new[] 用于为数组分配内存——但几乎可以肯定,完全忽略整个混乱局面会更好。
The new operator is what you normally use to create an object from the free store:
new 运算符通常用于从免费存储创建对象:
my_class *x = new my_class(0);
The difference between the two is that operator new justallocates raw memory, nothing else. The new operator starts by using operator new to allocate memory, but then it invokes the constructor for the right type of object, so the result is a real live object created in that memory. If that object contains any other objects (either embedded or as base classes) those constructors as invoked as well.
两者的区别在于 operator new只分配原始内存,没有别的。new 操作符首先使用 operator new 分配内存,然后它调用正确类型的对象的构造函数,因此结果是在该内存中创建的真实活动对象。如果该对象包含任何其他对象(嵌入的或作为基类),则这些构造函数也被调用。
回答by Austin Hyde
"operator new"
“新操作员”
class Foo
{
public:
void* operator new( size_t );
}
"new operator":
“新运营商”:
Foo* foo = new Foo();
In this example, new Foo()
calls Foo::operator new()
在这个例子中,new Foo()
调用Foo::operator new()
In other words, "new operator" calls "operator new()
" just like the + operator calls operator +()
换句话说,“new 运算符”调用“ operator new()
”就像 + 运算符调用operator +()
回答by Naveen
Following is the quote from More Effective C++ book from Scott Meyers:
以下是 Scott Meyers 的 More Effective C++ 书中的引述:
The new operator calls a function to perform the requisite memory allocation, and you can rewrite or overload that function to change its behavior. The name of the function the new operator calls to allocate memory is operator new.
new 运算符调用一个函数来执行必要的内存分配,您可以重写或重载该函数以更改其行为。new 运算符调用以分配内存的函数的名称是 operator new。
回答by AnT
There's no difference between "new operator" and "operator new". Both refer to the same thing: the overloadable/replaceable operator new
function that typically performs raw memory allocation for objects created by new-expressions.
“new operator”和“operator new”之间没有区别。两者都指同一件事:可重载/可替换operator new
函数,通常为new-expressions创建的对象执行原始内存分配。
Note also that neitherterm is present in the language specification (which is the defining source of the official terminology).
还要注意,这两个术语都没有出现在语言规范中(这是官方术语的定义来源)。
When you use new
in your program to create an object, it is called new-expression. New-expressionconsists of keyword new
and additional syntactic parts defined by the grammar. No part of this expression's syntax is ever referred to as an "operator".
当您new
在程序中使用来创建对象时,它被称为new-expression。New-expression由关键字new
和语法定义的附加句法部分组成。此表达式的语法中没有任何部分被称为“运算符”。
The raw memory allocation function operator new
is officially referred to as just "operator new
function". Note that the words operator
and new
in this sequence are just two separate C++ language keywords. They don't form an English term "operator new". Nowhere in the language specification you'll find any references to "operator new" as an English term. Every time this is just a combination of two independent keywords that produce declaration syntax for a memory allocation function.
原始内存分配函数operator new
正式简称为“operator new
函数”。注意的话operator
,并new
在此序列只是两个独立的C ++语言的关键字。它们不构成英语术语“operator new”。在语言规范中,您找不到任何将“operator new”作为英语术语的引用。每次这只是两个独立关键字的组合,为内存分配函数生成声明语法。
Again, in resume: formally in C++ there's no such English language terms as "operator new" or "new operator". The former sequence is present in the language specification as a mere combination of keywords, not as an English term. The latter is not present at all.
同样,在简历中:在 C++ 中,没有正式的英语术语“operator new”或“new operator”。前一个序列仅作为关键字的组合出现在语言规范中,而不是作为英语术语。后者根本不存在。
回答by Chris Fulstow
When you create a new object the memory is allocatedusing operator newthen the constructor is invoked to initialisethe memory. The new operatordoes both the allocation and the initialisation, where as the operator new only does the allocation.
当你创建一个新对象时,内存是使用operator new分配的,然后调用构造函数来初始化内存。在新的运营商确实都分配和初始化,那里的运营商新的只做分配。
回答by Gob00st
The OP's question is not phrased properly. It's better to phase as 'Difference between 'operator new' and 'new expression'?' Note 'operator new' often refers to 'operator new function' as well.
OP 的问题措辞不正确。最好将阶段定义为“'operator new'和'new expression'之间的区别?” 注意“operator new”通常也指“operator new function”。
And there are plenty correct answer around, below is mine:
周围有很多正确的答案,以下是我的:
1> 'new expression' call 'operator new' to allocate raw memory,then call constructor
1> 'new expression' 调用 'operator new' 来分配原始内存,然后调用构造函数
apple * p = new apple(); //new expression
2> 'operator new' only allocate raw memory, not much difference than malloc
2> 'operator new'只分配原始内存,和malloc差别不大
void* mem = operator new(sizeof(apple)); //just like calling malloc()
apple* p2 = new(mem) apple(1); //call construct here using placement new.
回答by aJ.
The new operator: C++ supports dynamic allocation of objects using the new operator. The new operator allocate memory for objects from a pool called the free store. The new operator calls the special function operator new.
new 运算符:C++ 支持使用 new 运算符动态分配对象。new 运算符从称为空闲存储的池中为对象分配内存。new 运算符调用特殊函数运算符 new。
operator new: If the request is for zero bytes of storage, operator new returns a pointer to a distinct object (that is, repeated calls to operator new return different pointers). If there is insufficient memory for the allocation request, operator new returns NULL or throws an exception. The first argument to operator new must be of type size_t (a type defined in STDDEF.H), and the return type is always void *.
operator new:如果请求是零字节的存储,operator new 返回一个指向不同对象的指针(即重复调用 operator new 返回不同的指针)。如果分配请求的内存不足,operator new 将返回 NULL 或抛出异常。operator new 的第一个参数必须是 size_t 类型(在 STDDEF.H 中定义的类型),并且返回类型始终为 void *。
Here is a MSDN links for more details:
这是一个 MSDN 链接以获取更多详细信息:
回答by njuhgn
newis an operator as well as a keyword.
see [1] In 2.13 && In 2.12.
newdoes two things: T* t = new T(arg);
1)allocate memory for the object: void* ptr = operator new(sizeof(T));
//operator newis a function(just like malloc in c), not an operator.(see [1] In 3.7.4 ). But Item 7 [2] said it is an operator too. In my opinion, the difference between operator and function is small, and you can see it when you recall that overloading operator is implemented by functions.
//we can overload this operator/function(operator new) doing what we want just here.
2)initialize the object in the allocated memory: call T::T(arg) on ptr
//onlycompiler can do this. Neither me nor you can.
//also compiler will invoke member objects' constructors and base class's constructor if T has them. And this invocation is recursive. Only compiler can do that.
So, operator new does part of the missions of new, and only in this part we can do something.
[1]: ISO/IEC, N3690. http://ww.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf
[2]: Meyers, Scott. Effective C++, 3rd.
new是一个运算符,也是一个关键字。
参见 [1] 在 2.13 中&& 在 2.12 中。
new做了两件事:T* t = new T(arg);
1)为对象分配内存: void* ptr = operator new(sizeof(T));
// operator new是一个函数(就像 c 中的 malloc 一样),而不是一个运算符。(参见 [1] In 3.7.4 )。但是条款 7 [2] 说它也是一个操作符。在我看来,operator 和 function 的区别很小,回想一下 operator 是由函数实现的重载就可以看出来了。
//我们可以在这里重载这个操作符/函数(operator new)做我们想做的事情。
2)在分配的内存中初始化对象:在ptr上调用T::T(arg)
//只有编译器可以做到这一点。我和你都不能。
//如果 T 有成员对象的构造函数和基类的构造函数,编译器也会调用它们。这个调用是递归的。只有编译器可以做到这一点。
所以,operator new 完成了 new 的部分任务,只有在这部分我们才能有所作为。
[1]:ISO/IEC,N3690。http://ww.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf
[2]:迈耶斯,斯科特。有效的 C++,第 3 名。