Overload the new operator

Source: Internet
Author: User

I. Heavy Load rules

I. Operators that can be overloaded

+-*/%
^ & | ~ !
==>< + =-=
* =/= % = ^ = & =
|=>><>>=<=
=! ==<= &&
| ++ ---> *,
-> [] () Operator new []
Operator Delete operator Delete []

Ii. Operators that cannot be overloaded

::..*? :
Sizeoftypeidnewdelete
Static_castdynamic_castconst_castreinterpret_cast

III. Basic Rules

1. The unary operator can be a member function [1] without parameters or a non-member function [1] with a parameter.
2. Binary operators can be member functions with a parameter [1] or non-member functions with two parameters [1].
3. Operator =, operator [], operator (), operator-> can only be defined as a member function [1].
4. the return value of operator-> must be a pointer or an object that can use->.
5. When operator ++ and operator are reloaded, an int parameter is included to indicate the suffix, and no parameter is included to indicate the prefix.
6. Except operator new and operator Delete, at least one non-built-in data type must be included in the overloaded operator parameters.
7. x @ Y the search range is: x member functions --> Functions in global functions/functions in the namespace where X is located/functions in the namespace where Y is located/Friends of X
Number/y.
8. The overloaded operators should try to simulate the behavior of the operators to build types internally.

Ii. Heavy Load


I. Some suggestions on Operator Overloading

1. only the operator (such as: + =) that will change the value of the first parameter is defined as a member function, and the operator (such as: +) of a new object will be returned) it is defined as a non-member function (and implemented using ++ = ).
2. Only non-member functions can implement gender conversion on the left parameter. If conversion is required, the operator should be defined as a non-member function.
3. For the unary operator, it is best to overload it as a member function to avoid implicit conversion.
4. For binary operators, in order to be able to perform the same implicit conversion as the right operand on the left operand, it is best to overload them as non-member functions.
5. Operator >>and operator <<should be defined as non-member functions to comply with the usage habits.
6. If operators such as operator [] are reloaded, the const version and non-const version should be provided as much as possible.
7. For the definition of operators as members or non-members, refer to the following suggestions:
Operator Creation
All unary operator members
= () []-> Must be a member
+ =-=/= * = ^ = & =! ==>>=<= Member
Non-member of other binary Operators

8. If the default operator can already be applied to your type, avoid overloading the operator, such as operator, operator & (get address), and so on.

Ii. Heavy Load operator new

1. Why reload operator new?

[Efficiency problem] The system usually provides a very slow distributor speed by default, and the waste of allocating small objects is serious.
[Change behavior] if the default distributor fails, an exception is thrown. You may want to change this behavior.

2. Operator new action

[Differentiate three different New]
New operator (new expression, new operator, new expression): This operator is usually used when we call X * PX = new X. It is built in the language and cannot be overloaded, it cannot change its behavior. it includes operator New for memory allocation and placement New for calling constructors.
Operator New: opeator new is a function, void * operator new (size_t size ). it allocates memory of the specified size and can be overloaded. You can add additional parameters, but the first parameter must be size_t. in addition to being called by new operator, operator new can also be called directly: void * rawmem = Operator new (sizeof (x )).
Placement New: Placement new uses constructor on a specified memory, including the header file <New>. You can also directly use placement New: x * PX = new (rawmem) X. [2]

Similar to new operator, for Delete operator, operator Delete: void operator Delete (void *) also exists. The Destructor PX-> ~ X ().

[Operator new error handling]
The default operator new will throw the STD: bad_alloc exception when memory allocation fails; nothrow new [3]
(X * PX = new (nothrow) x) 0 is returned when memory allocation fails. this behavior can be changed by setting new-handler. new-handler is a callback function pointer, typedef void (* new_handler) (void ). after the callback handle is set through the set_new_handler (new_handler) function, if the memory allocation fails, operator new will continuously call the new-handler function until enough memory is found. to avoid endless loops, the new-handler function must have one of the following actions:
(1). Find available memory.
(2) install other new-handler functions.
(3). Remove New-handler, that is, set_new_hanlder (0). In this way, the default action will be resumed in this loop, and an exception will be thrown or 0 will be returned.
(4). Throw an exception.
(5) Save the error log and exit the program.

3. Prepare to reload operator new

When operator new is reloaded, it must be compatible with the default operator new error handling method. in addition, C ++ Standard specifies that a valid memory address should be returned when the required memory is 0 bytes. therefore, the implementation of operator new overload should be as follows:
Void *... operator new (size_t size ...)
{
If (size = 0)
Size = 1;

While (1)
{
... // Allocate memery
If (allocate sucessfull)
Return... // return the pointer.

New_handler curhandler = set_new_handler (0 );
Set_new_handler (curhandler); // get current new handler

If (curhandler = 0)
(* Curhandler )();
Else
Throw STD: bad_alloc ();
}
}
Reload operator Delete is much simpler. You only need to note that C ++ standard requires that deleting a null value is safe.

4. Reload operator new

The overload of opeator new is very different from that of other operators. first, the default operator new can be applied to your custom type, even if you do not reload it. second, when other operators are overloaded, the number of parameters is fixed, while the number of operator new parameters can be arbitrary. You only need to ensure that the first parameter is size_t and the return type is void, in addition, the overloaded parameter types do not need to include custom types. in general, operator new is more like a function overload than an operator overload.

[★Reload operator new with different parameters]

Operator new can be reloaded by using different parameter types, for example:
Void * operator new (size_t size, int X, int y, int Z)
{
...
}

X * PX = new (1, 2, 3) X;
You can also use the default value for operator new overloading. The principle is the same as that for normal function overloading, as long as there is no conflict with the existing form. you may have thought that you can even use the variable parameter in operator new, if you really need it.
Void * operator new (size_t size, int X, int y = 0, int z = 0)
{
...
}

X * PX = new (10) X;
Y * py = new (10, 10) y;
Z * PZ = new (10, 10, 10) Z;

...
Void * operator new (size_t size ,...)
...
You can also directly reload the void * operator new (size_t size) function in the global space. This will change the behavior of all default new operators and is not recommended.

[★Reload the class's exclusive operator New]

When a class is overloaded with operator new, it must be defined as a static function of the class [4], because operator new will be called before the class object is constructed. that is to say, when operator new is called, this pointer does not exist, so the overloaded operator new must be static. of course, you can add additional parameters and use the default value to overload operator new in the class. in addition, like normal functions, operator new can also be inherited.
Class X {
...
Static void * operator new (size_t size); //... (1)
Static void * operator new (size_t size, INT); //... (2)
};

Class Y: Public X {
...
};

Class Z: Public X {
...
Static void * operator new (size_t size); //... (3)
};

X * px1 = new x; // call (1)
X * px2 =: New X; // call default operator new
X * px3 = new (0) x; // call (2)

Y * py1 = New Y; // call (1)

Z * pz1 = new Z; // call (3)
Z * PZ2 =: new Z; // call default operator new
Z * pz3 = x: new Z; // error, no way to call (1)
Z * pz4 = new (0) Z; // error, no way to call (2)

5. Reload operator Delete

If you reload an operator new, remember to reload the operator Delete in the same range. because only you can know how to release the allocated memory. if you forget it, the compiler will not prompt you, And it will use the default operator Delete to release the memory. the cost of this kind of forgetting is heavy. You have to write operator delete while writing operator new.
If operator Delete is used in the class, it must also be declared as a static function, because when operator Delete is called, the object has been destructed. Operator Delete overload can be in two forms:
(1) void operator Delete (void * MEm)
(2) void operator Delete (void * MEM, size_t size)
In addition, operator Delete can both exist. When Delete PX is called, if expression (1) exists, operator Delete (1) is called. the (2) type is called only when the (1) type does not exist. for operator Delete in the form of (2), if the base class pointer is used to delete the derived class object, and the base class destructor is not virtualized, the size value may be incorrect.

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.