473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Why these operators cant be overloaded?

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
14 3620
erm
you can overload operators new and delete

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

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

<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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andy Jarrell | last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain overloaded operators don't seem to come with the inheritance. For example: ...
3
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am...
20
by: Brad Eck | last post by:
"The only operators that cannot be overloaded are :: (scope resolution), . (member selection), and .* (member selection through pointer to function). Quoting from Stroustrup's 3rd edition of _The...
4
by: masood.iqbal | last post by:
Please help me with this doubt that I have regarding overloaded operators. Sometimes they are member functions and sometimes they are friends (e.g. see the code snippet from Stroustrup, Second...
10
by: maadhuu | last post by:
hi i wasnt to know the answer for the following. now ,u can overload all the operators which are basically determined at runtime (coz' of whch operators like sizeof())cannot be overloaded. now...
0
by: Syanide | last post by:
here's a bit info for you fellas: eg.. you have Square classes u wanna add public static int operator+ (Square s1, Square s2) { // your code here } comparision operators..
3
by: Silver Phoenix | last post by:
Greetings. I have some questions that I would like to clear out. 1. Why do operators have to be static? (This question relates to the language specification) 2. More importantly, couldn't...
5
by: Suman | last post by:
Having had a look at the C++ FAQ, comp.lang.c++ & comp.std.c++ archives and Stroustrup's FAQs (particularly the following: <url:http://www.research.att.com/~bs/bs_faq2.html#overload-dot/>) I am...
2
by: velvizhi | last post by:
why the operators like scope resolution operator,conditional operator,size of operator cant be overloaded?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.