Why the overloaded new operator is implicitly static and does not require the permission of the region to build the object

Why is the overloaded new operator implicitly static and how can we allocate memory by invoking the overloaded new operator without the scope resolution operator?

In my opinion, if something is static, we can call it mainly through the class name.

class xyz { void* operator new (size_t size); //implicitly declared static void operator delete (void *p); //implicitly declared static }; int main() { C *p = new C; delete p; } 
+7
c ++ new-operator
source share
2 answers

The draft C ++ standard says in Section 5.3.4 New Clause 9, that if a new expression does not start with :: , then they first looked at the scope, and then, if they were not found globally:

If a new expression begins with a unary :: operator, the distribution of the function name is looked up globally. Otherwise, if the distributed type is the type of the class T or its array, the distribution of the function name is scanned in area T. If this search is not performed to find the name, or if the selected type is not a type of class, the name of the distribution function is displayed in the global area

Due to the fact that this is implicitly static, it would seem like an inconvenient restriction to require an instance of a type to call the element distribution function. It seems like it will also require a different syntax, as the compiler finds out which instance will be used, which would make things messy.

The fact that element distribution functions are implicitly static is discussed in Section 12.5 free store:

Any distribution function for class T is a static member (even if not explicitly declared static).

+5
source share

All operators are implicit. You do not have to use the scope operator for all other operators.

Think about how unpleasant it would be:

 int a = 4 int::operator* 6; 

And for this very reason, they did it that way.

In addition, all operators are analyzed in lexical code processing. The value of these literals can be defined by the user:

Section 2.14.8 discusses the literal rules:

A user literal is considered as a call to a literal operator or a literal operator pattern (13.5.8). To determine the form of this call for a given user literal L with ud-suffix X, literal-operator-id, whose literal suffix identifier is X, is looked up in context L using the rules for finding unqualified names (3.4.1). Let S be the set of ads found by this search. S should not be empty.

+1
source share

All Articles