473,895 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Operators that cannot be Overloaded - WHY?

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

First, I've seen ?: and sizeof added to this list. Someone also
mentioned 'delete' but Stroustrup doesn't refer to it as one. In fact,
it's in the list of overloadable operators.

Second, what is meant by 'take a name'? What is meant by 'subtleties'?
Any thoughts as to the detailed reasoning behind these?
Jul 22 '05 #1
20 37416
"Brad Eck" <br******@sites dynamic.com> wrote...
"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.'"

First, I've seen ?: and sizeof added to this list.
The ternary op is excluded probably due to complexity of its relation
to the assignment op. As to sizeof, ::, they're not really operators
in the true sense, I guess.
Someone also
mentioned 'delete' but Stroustrup doesn't refer to it as one. In fact,
it's in the list of overloadable operators.
'delete' can be overloaded. Whoever mentioned it probably didn't know
what he/she was talking about.
Second, what is meant by 'take a name'? What is meant by 'subtleties'?
Well, if you overload the . (member access) then the only way to access
a member would be (&obj)->member. Awkward. Now, if both '&' and -> are
overloaded, what do you get?...
Any thoughts as to the detailed reasoning behind these?


Detailed, 'fraid not. Try "Design and Evolution of C++". There is
a whole chapter on overloading.

V
Jul 22 '05 #2

"Brad Eck" <br******@sites dynamic.com> wrote in message
news:5e******** *************** **@posting.goog le.com...
"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.'"

First, I've seen ?: and sizeof added to this list. Someone also
mentioned 'delete' but Stroustrup doesn't refer to it as one. In fact,
it's in the list of overloadable operators.

Second, what is meant by 'take a name'? What is meant by 'subtleties'?
Any thoughts as to the detailed reasoning behind these?


Most normal operator take values, e.g. operator+ might add two integer
values. But the right hand side of operators . .* and :: are names of
things, e.g. name of a class member. Left hand side of :: is a name as well.
Difference between a name and a value seems pretty clear to me, and I guess
the subtleties would be the fact that nowhere else in C++ are you allow to
manipulate the names of class members, variables etc.

john
Jul 22 '05 #3
Victor Bazarov wrote:
'delete' can be overloaded. Whoever mentioned it probably didn't know
what he/she was talking about.


Delete CANNOT be loaded. It's just syntactic stupidity that makes the
deallocation function called "operator delete".
Jul 22 '05 #4
John Harrison wrote:
Most normal operator take values, e.g. operator+ might add two integer
values. But the right hand side of operators . .* and :: are names of
things, e.g. name of a class member.


operator-> works with a member on the rhs.
Jul 22 '05 #5

"Ron Natalie" <ro*@sensor.com > wrote in message
news:41******** *************** @news.newshosti ng.com...
John Harrison wrote:
Most normal operator take values, e.g. operator+ might add two integer
values. But the right hand side of operators . .* and :: are names of
things, e.g. name of a class member.


operator-> works with a member on the rhs.


Use but in terms of how it is overloaded, operator-> is a unary operator
which returns a pointer, to which operator-> is reapplied.

Now Bjarne could have made it that operator. is overloaded as a unary
operator which returns a reference to which operator. is then reapplied. He
didn't and I don't know the reason but it seems like a reasonable decision
to me.

john
Jul 22 '05 #6
John Harrison wrote:
"Ron Natalie" <ro*@sensor.com > wrote in message
news:41******** *************** @news.newshosti ng.com...
John Harrison wrote:

Most normal operator take values, e.g. operator+ might add two integer
values. But the right hand side of operators . .* and :: are names of
things, e.g. name of a class member.


operator-> works with a member on the rhs.

Use but in terms of how it is overloaded, operator-> is a unary operator
which returns a pointer, to which operator-> is reapplied.

Now Bjarne could have made it that operator. is overloaded as a unary
operator which returns a reference to which operator. is then reapplied. He
didn't and I don't know the reason but it seems like a reasonable decision
to me.


class SomeClass {};

class HasArrowOverloa ded {
public:
SomeClass* operator->();
};

class HasDotOverloade d {
HasArrowOverloa ded& operator &();
SomeClass& operator .(); // impossible in current C++, but
// let's pretend it's OK
void foo();
};

int main() {
HasDotOverloade d hdo;
// I want to call 'foo' for 'hdo'. How?
}

V
Jul 22 '05 #7
Ron Natalie wrote:
Victor Bazarov wrote:
'delete' can be overloaded. Whoever mentioned it probably didn't know
what he/she was talking about.

Delete CANNOT be loaded. It's just syntactic stupidity that makes the
deallocation function called "operator delete".


Whatever you call that ("syntactic stupidity" or "semantic brilliance")
you still can do

class RonDoesntLikeMe BooHoo {
public:
void operator delete(void*, size_t);
};

and when you say

RonDoesntLikeMe BooHoo *foo;
...
delete foo;

the _overloaded_ function will be called. And it has the word "operator"
in its name. If that means that it "CANNOT be loaded", then I'm the last
Chinese emperor.

V
Jul 22 '05 #8
Victor Bazarov wrote:
John Harrison wrote:
"Ron Natalie" <ro*@sensor.com > wrote in message
news:41******** *************** @news.newshosti ng.com...
John Harrison wrote:
Most normal operator take values, e.g. operator+ might add two integer
values. But the right hand side of operators . .* and :: are names of
things, e.g. name of a class member.
operator-> works with a member on the rhs.


Use but in terms of how it is overloaded, operator-> is a unary operator
which returns a pointer, to which operator-> is reapplied.

Now Bjarne could have made it that operator. is overloaded as a unary
operator which returns a reference to which operator. is then
reapplied. He
didn't and I don't know the reason but it seems like a reasonable
decision
to me.

class SomeClass {};

class HasArrowOverloa ded {
public:
SomeClass* operator->();
};

class HasDotOverloade d {
HasArrowOverloa ded& operator &();
SomeClass& operator .(); // impossible in current C++, but
// let's pretend it's OK
void foo();
};

int main() {
HasDotOverloade d hdo;
// I want to call 'foo' for 'hdo'. How?
}

V

hdo.HasDotOverl oaded::foo() ?

--
Regards,
Slava

Jul 22 '05 #9
Victor Bazarov posted:
Ron Natalie wrote:
Victor Bazarov wrote:
'delete' can be overloaded. Whoever mentioned it probably didn't know what he/she was talking about.

Delete CANNOT be loaded. It's just syntactic stupidity that makes the deallocation function called "operator delete".


Whatever you call that ("syntactic stupidity" or "semantic

brilliance") you still can do

class RonDoesntLikeMe BooHoo {
public:
void operator delete(void*, size_t);
};

and when you say

RonDoesntLikeMe BooHoo *foo;
...
delete foo;

the _overloaded_ function will be called. And it has the word "operator" in its name. If that means that it "CANNOT be loaded", then I'm the last Chinese emperor.

V

Now that's the kind of ridiculing debate I like to see!

Handbags at dawn, ladies!
-JKop
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
17888
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. Why can't the = (assignment) operator be overloaded as a friend function ? I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it said the following :
3
1504
by: TJ | last post by:
Hi, I've been referring to many codes and books and always see that the stream insertion operators are overloaded as friends. Why is that? Are there any other overloading that need the same type of specification, as friends in the class that will use them? Thanks, TJ
4
12915
by: Bangalore | last post by:
Hi, Can anybody explain , why some operators(::, ., .*, sizeof,?:) cannot be overloaded in C++? Thanks
44
2460
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case statement: void print_structs(void * struct_argument, enum struct_type stype) { switch(stype) { case STRUCT_TYPE_1:
7
2155
by: Sacha Faust | last post by:
if you have a ready only properly like string.Length and I do --(string.Length) I get a compiler problem saying that I can't modify the value of the readonly property. From my understanding, the -- operator should be decrementing the value return by (string.Lenght) and not string.Lenght directly. Am I right or just completely wrong on this and should get more sleep. If I'm wrong can you give details.
5
2421
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<>...
4
1549
by: moleskyca1 | last post by:
What operators cannot be virtual and why? I looked at FAQ and found nothing. I think there are operators that cannot be virtual, but I don't know why?
19
2315
by: Jess | last post by:
Hello, After seeing some examples about operator overloading, I'm still a bit confused about the general syntax. The following is what I think, not sure whether it's correct. 1. For a unary operator that's a member of a class, its form is usually "operatorP()" (where P is the operator's name).
5
2660
by: puzzlecracker | last post by:
I don't recall whether operators, which are members of the class, are intherited in subclasses? thanks
2
18089
by: velvizhi | last post by:
why the operators like scope resolution operator,conditional operator,size of operator cant be overloaded?
0
9990
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
9835
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
11249
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10845
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...
1
10935
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10473
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...
0
5863
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
6068
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4285
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.