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 }}