473,805 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_cas t .

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
14 3663
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_cas t

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*********@gma il.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_cas t


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::Memb er 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*********@gm ail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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_cas t .

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
6890
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: // TestA.h --------------------------------------- #include <iostream> enum Aval { FIRST_VALUE,
3
9420
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 presenting below a summary of what I have gathered. I would appreciate if someone could point out to something that is specific to Borland C++ and is not supported by the ANSI standard. I am also concerned that some of the information may be outdated...
20
37403
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 C++ Programming Language_, section 11.2 (page 263), these three operators 'take a name, rather than a value, as their second operand and provide the primary means of referring to members. Allowing them to be overloaded would lead to subtleties.'"...
4
1622
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 Edition that I have posted to comp.sources.d). How do we decide which is more appropriate? Why are the overloaded "<<" and ">>" operators always friends? Also, what is an appropriate application for the overloaded function call operator?
10
1989
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 my doubt is , if u have something like p->a ....where p(say) is a pointer of a user defined type, then u can overload this because p is determined at runtime ???is this right ??? also if "a" is an object of the some user defined class,
0
1725
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
1161
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 there be operators that were tranlated at compile-time to "normal" methods?
5
2418
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 left to wondering why Stroustrup doesn't mention the *_cast<> operators or operator .* in that particular FAQ. Archived discussions have convinced me that .* _cannot_ be overloaded. I have gathered a notion (perhaps incorrectly) that the *_cast<>...
2
18085
by: velvizhi | last post by:
why the operators like scope resolution operator,conditional operator,size of operator cant be overloaded?
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10359
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10104
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7645
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5541
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.