By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,495 Members | 1,322 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,495 IT Pros & Developers. It's quick & easy.

Why these operators cant be overloaded?

P: n/a
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?

Aug 11 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
erm
you can overload operators new and delete

Aug 11 '05 #2

P: n/a
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.

Aug 11 '05 #3

P: n/a
Ian
am*********@gmail.com wrote:
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
Aug 11 '05 #4

P: n/a
Le jeudi 11 août 2005 à 10:33, Ian a écrit dans comp.lang.c++*:
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?


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
Aug 11 '05 #5

P: n/a

"Serge Paccalin" <sp@mailclub.no.spam.net.invalid> wrote in message
news:11****************@canttouchthis-127.0.0.1...
Le jeudi 11 août 2005 à 10:33, Ian a écrit dans comp.lang.c++ :
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?


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 -
Aug 11 '05 #6

P: n/a
Serge Paccalin wrote:
Le jeudi 11 août 2005 à 10:33, Ian a écrit dans comp.lang.c++ :
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?


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.

Aug 11 '05 #7

P: n/a
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?

----------------------------------------------------------------------------------------------------------------------------

I am desperately looking for answer . Will anybody help me on this?

Aug 11 '05 #8

P: n/a

<am*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
As you know there are few operators in C++ which cant be overloaded.

., .*, ::, ?: , 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?


http://www.research.att.com/~bs/bs_f...l#overload-dot

- Risto -
Aug 11 '05 #9

P: n/a
erm wrote:
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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 11 '05 #10

P: n/a
Risto Lankinen wrote:

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

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 11 '05 #11

P: n/a
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.

Aug 11 '05 #12

P: n/a
Le jeudi 11 août 2005 à 10:49, Risto Lankinen a écrit dans
comp.lang.c++*:
And the difference is?
The new operator first calls operator new() and then the constructor(s).


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
Aug 11 '05 #13

P: n/a
am*********@gmail.com 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

Aug 12 '05 #14

P: n/a
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.

<am*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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?

Aug 12 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.