Operator New "vs." new operator "relationship and Difference

Source: Internet
Author: User

Link: http://blog.csdn.net/zhangxaochen/article/details/8033503

Here is an example:

"Operator New ":

class Foo { public:         void* operator new( size_t ){cout<<"in Foo's operator new"<<endl;return ::operator new(t);}Foo(){cout<<"in Foo's ctor"<<endl;}}; 

Note that the parameter type of operator new is size_t. This function is used to allocate memory, which is about equal to malloc and returns void * instead of Foo *. It is indeed different from "new operator.

"Operator new" is actually a function. With a global scope version, you can also overwrite your own member function version in the class. If the member function version is overwritten, The Global version will be hidden when new something is used.

"New operator ":

Foo* foo=new Foo();

"New operator", some people say there is no such term at all. In the C ++ standard, although new/delete and sizeof are the same keywords and are used in similar ways, new/delete cannot be called "operator", but is called "expression ", refer to the discussion (http://stackoverflow.com/questions/1885849), but who knows, It is also said that this is one of the areas of C ++ standards that are not clearly stated. It is said that more than tive C ++ has used the "new operator" statement:

The new operator calla 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 cballs to allocate memory is Operator new.

And msdn also uses "new operator": http://msdn.microsoft.com/en-us/library/kftdy56f%28VS.71%29.aspx

So let him do it .. There is such a term ..

The above Foo * Foo = new Foo (); during the execution of this sentence, actually two functions, operator new and constructor, were called successively. Operator new allocates sizeof (FOO) memory and constructor initializes it. Therefore, the output is as follows:

In Foo's operator new

In Foo's ctor

Link: http://blog.csdn.net/zhangxaochen/article/details/8033503

{Over }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.