Why these operators cant be overloaded?

A

ambar.shome

Hi,

As you know there are few operators in C++ which cant be overloaded.

They are:

.., .*, ::, ?: , new , delete , sizeof , typeid , static_casr ,
dynamic_cast , const_cast , reinterpret_cast .

Theremust be some reason for this restriction for each of the
operators.

Does anyone know the exact reason for such restriction on each of them?
 
A

ambar.shome

I dont think "new" can be overloaded. "operator new" can be overloaded
but not "new" operator. Same for delete. Follow any traditional book
for confirmation.
 
I

Ian

I dont think "new" can be overloaded. "operator new" can be overloaded
but not "new" operator. Same for delete. Follow any traditional book
for confirmation.
And the difference is?

You can have your own class local operator new and your own global one.

Ian
 
S

Serge Paccalin

Le jeudi 11 août 2005 à 10:33, Ian a écrit dans comp.lang.c++ :
And the difference is?

The new operator first calls operator new() and then the constructor(s).

--
___________ 11/08/2005 10:37:58
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
 
R

Risto Lankinen

Serge Paccalin said:
Le jeudi 11 août 2005 à 10:33, Ian a écrit dans comp.lang.c++ :


The new operator first calls operator new() and then the constructor(s).

Very artificial distinction.

Or, similarly it could be argued that "->" operator cannot be overloaded,
but "operator->" can (because the "->" operator first calls "operator->"
and then the "->" operator).

- Risto -
 
M

Maxim Yegorushkin

Serge said:
Le jeudi 11 août 2005 à 10:33, Ian a écrit dans comp.lang.c++ :


The new operator first calls operator new() and then the constructor(s).

There are new statement and operator new (the same applies to delete).
new statement is a language construct that calls operator new and then
an object constructor. new statement can not be redefined or overloaded
(macro aside), but operator new can be overloaded.

The same name for language construct and operator has lead to much
confusion.
 
A

ambar.shome

Hi,

I think you all are not following my original question. Once again
please follow my query given below:
---------------------------------------------------------------------------------------------------------------------------------
As you know there are few operators in C++ which cant be overloaded.


They are:


.., .*, ::, ?: , new , delete , sizeof , typeid , static_casr ,
dynamic_cast , const_cast , reinterpret_cast .


Theremust be some reason for this restriction for each of the
operators.


Does anyone know the exact reason for such restriction on each of them?
 
P

Pete Becker

erm said:
you can overload operators new and delete

Well, yes and no. You can overload operator new and operator delete, but
you cannot overload the new and delete operators, which was what the
original question was about.

operator new and operator delete are functions: void *operator
new(size_t) and void operator delete(void*) in their simplest forms.
When you say 'new int' you're using the new operator to create an int on
the heap; it calls operator new to get the memory, then constructs the
value as appropriate for the type. Similarly, when you say 'delete ptr'
you're using the delete operator, which destroys the value and then
calls operator delete to release the memory.
 
P

Pete Becker

Risto said:
Very artificial distinction.

It may be artificial, but it's exactly what the standard says. The
problem is the confusing names 'operator new', which is a function and
can be overloaded, and the 'new' operator, which is a keyword and does
what's described above.
 
A

ambar.shome

Hi,

Thank you very much for the link. But there are few operators left on
which I dint get any info. Those are

typeid , static_casr ,
dynamic_cast , const_cast , reinterpret_cast

Do u or anyone have any idea about them?

Again thanking you for the link.
 
S

Serge Paccalin

Le jeudi 11 août 2005 à 10:49, Risto Lankinen a écrit dans
comp.lang.c++ :
Very artificial distinction.

ISO-IEC 14882:1998, page 78:

8 A /new-expression/ obtains storage for the object by calling an
/allocation function/ (3.7.3.1). If the /new-expression/ terminates
by throwing an exception, it may release storage by calling a
deallocation function (3.7.3.2). If the allocated type is a
non-array type, the allocation function's name is 'operator new'
and the deallocation function's name is 'operator delete'. If the
allocated type is an array type, the allocation function's name is
'operator new[]' and the deallocation function's name is 'operator
delete[]'. [/Note:/ an implementation shall provide default
definitions for the global allocation functions (3.7.3, 18.4.1.1,
18.4.1.2). A C++ program can provide alternative definitions of
these functions (17.4.3.4) and/or class-specific versions (12.5). ]
Or, similarly it could be argued that "->" operator cannot be overloaded,
but "operator->" can (because the "->" operator first calls "operator->"
and then the "->" operator).

This is just silly.

--
___________ 11/08/2005 15:16:50
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
 
M

msalters

(e-mail address removed) schreef:
Hi,

Thank you very much for the link. But there are few operators left on
which I dint get any info. Those are

typeid , static_casr ,
dynamic_cast , const_cast , reinterpret_cast

Casts can't be overloaded because you can define cast operators on
your own types. E.g.

class X{};
class Y {
operator X();
};

typeid/dynamic_cast are related to RTTI, which is implemented using'
compiler magic. For that reason, it can't be overloaded. That's similar
to new (calling constructors is compiler magic too).

HTH,
Michiel Salters
 
G

Girish Shetty

Operator . (dot) could in principle be overloaded using the same technique
as used for ->.
However, doing so can lead to questions about whether an operation is meant
for the
object overloading. or an object referred to by .

class Y {
public:
void f();
// ...
};

class X { // assume that you can overload .
Y* p;
Y& operator.() { return *p; }
void f();
// ...
};

void g(X& x)
{
x.f(); // X::f or Y::f or error?
}


In ClassName::Member neither ClassName nor Member are expressions with
values;
ClassName and Member are names known to the compiler and :: performs a
(compile time)
scope resolution rather than an expression evaluation.

One could imagine allowing overloading of x::y where x is an object rather
than a namespace or a class,
but that would contrary to first appearences - involve introducing new
syntax (to allow expr::expr).


?: is not allowed to overload because,
A function overloading expr1? expr2:expr3 would not be able to guarantee
that
only one of exper2 and expr3 was executed as its in the hand of programmer.

sizeof is not allowed to overload because,
A function overloading sizeof can return anything they want..!!!!

Sizeof cannot be overloaded because built-in operations, such as
incrementing a pointer into an array implicitly depends on it.

X a[10];
X* p = &a[3];
X* q = &a[3];
p++; // p points to a[4] thus the integer value of p must be sizeof(X)
larger than the integer value of q

Thus, sizeof(X) could not be given a new and different meaning by the
programmer without violating basic language rules.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top