473,513 Members | 2,493 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

which are the operators that can be overloaded ??

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,

then is (&a).member valid ?????I thought all "." cannot be overloaded
....why ???? (even in such cases)
Jul 23 '05 #1
10 1958
maadhuu wrote:
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 ???
No. If 'p' is a pointer (to anything), it's a built-in type. For it
operators like * -> + - += -= = == != are predefined and cannot be
overloaded.
also if "a" is an object of the some user defined class,

then is (&a).member valid ?????I thought all "." cannot be overloaded
...why ???? (even in such cases)


If 'a' is of some UDT, then that UDT can have operator & overloaded, and
returning an object of some other type for which the dot (.) operator is
used. It's not easy to get the address of that object if you overload the
operator&, but you may not need to have the address taken.

V
Jul 23 '05 #2
Victor Bazarov wrote:
maadhuu wrote:
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 ???

No. If 'p' is a pointer (to anything), it's a built-in type. For it
operators like * -> + - += -= = == != are predefined and cannot be
overloaded.


What are you talking about?!?

How is a pointer to a user defined class built-in?

also if "a" is an object of the some user defined class,

then is (&a).member valid ?????I thought all "." cannot be overloaded
...why ???? (even in such cases)

if u overload . how will you access class members?


If 'a' is of some UDT, then that UDT can have operator & overloaded, and
returning an object of some other type for which the dot (.) operator is
used. It's not easy to get the address of that object if you overload the
operator&, but you may not need to have the address taken.

V


I suggest you check out the excellent reference book on C++ by Bruce Eckel:

Thinking in C++ Vols 1 and 2:

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html

There is an entire chapter in Valume One on operator overloading.

the only operators you can't overload are . and .*
Jul 23 '05 #3
Aaron Gage wrote:
Victor Bazarov wrote:
maadhuu wrote:

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

No. If 'p' is a pointer (to anything), it's a built-in type. For it
operators like * -> + - += -= = == != are predefined and cannot be
overloaded.

What are you talking about?!?


C++. What are YOU talking about?
How is a pointer to a user defined class built-in?


It's a pointer. A pointer is a built-in type.

OK, let's not argue about this. Go ahead and try to overload operator+
for your pointer. I DARE YOU!
also if "a" is an object of the some user defined class,

then is (&a).member valid ?????I thought all "." cannot be overloaded
...why ???? (even in such cases)

if u overload . how will you access class members?


If 'a' is of some UDT, then that UDT can have operator & overloaded, and
returning an object of some other type for which the dot (.) operator is
used. It's not easy to get the address of that object if you overload the
operator&, but you may not need to have the address taken.

V

I suggest you check out the excellent reference book on C++ by Bruce Eckel:

Thinking in C++ Vols 1 and 2:

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html

There is an entire chapter in Valume One on operator overloading.

the only operators you can't overload are . and .*


Thanks. :-)

I suggest you read it yourself. And other books too. And pay attention
next time.

V
Jul 23 '05 #4

"Aaron Gage" <ga*****@hotmail.com> wrote in message
news:42********@news.melbourne.pipenetworks.com...

No. If 'p' is a pointer (to anything), it's a built-in type. For it
operators like * -> + - += -= = == != are predefined and cannot be
overloaded.


What are you talking about?!?

How is a pointer to a user defined class built-in?


He's talking about the pointer, not the class. Pointers are built-in types.
And you can't overload operators which operate on them. You can overload
the operators that operate on the class it points to, but not ones which
operate on the pointer itself.

In other words, this is legal:

bool operator ==(const MyClass& lhs, const MyClass& rhs)

as is this:

bool MyClass::operator ==(const MyClass& rhs)

but not this:

bool operator ==(MyClass* lhs, MyClass* rhs)
-Howard

Jul 23 '05 #5
Victor Bazarov wrote:
It's a pointer. A pointer is a built-in type.

OK, let's not argue about this. Go ahead and try to overload operator+
for your pointer. I DARE YOU!


Does this count?

#include <stdio.h>

class C {
public:
int x;
};

typedef C *P;

P operator+(const P &ptr, const C &val) {
printf("adding\n");
return 0;
};

int
main() {
P a;
C b;

a + b;
}

Jul 23 '05 #6
tedu wrote:
Victor Bazarov wrote:

It's a pointer. A pointer is a built-in type.

OK, let's not argue about this. Go ahead and try to overload operator+
for your pointer. I DARE YOU!

Does this count?

#include <stdio.h>

class C {
public:
int x;
};

typedef C *P;

P operator+(const P &ptr, const C &val) {
printf("adding\n");
return 0;
};

int
main() {
P a;
C b;

a + b;
}


Nice try. No, it doesn't count. You overloaded it for 'C'.
Do the same for P and an int, for example.

V
Jul 23 '05 #7

"tedu" <tu@zeitbombe.org> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Victor Bazarov wrote:
It's a pointer. A pointer is a built-in type.

OK, let's not argue about this. Go ahead and try to overload operator+
for your pointer. I DARE YOU!
Does this count?


No.

#include <stdio.h>

class C {
public:
int x;
};

typedef C *P;

P operator+(const P &ptr, const C &val) {
printf("adding\n");
return 0;
};

int
main() {
P a;
C b;

a + b;
}


That's not overloading the addition. You're just printing out some text (in
an overload for the class C, really).

Try actually adding something to ptr in the operator. Suppose, for
instance, you put "return ptr + val.x" there. What happens? Oh, it has to
add something to the pointer. So what does it do? It calls the built-in
operator for adding an integer to a pointer. The result isn't the value of
ptr plus val.x, it's the value of ptr plus (val.x times the size of C).

So, you need an operator that takes an int, not one that takes an object.
And the compiler won't let you define such an operator.

-Howard

Jul 23 '05 #8
i did not quite understand the above argument about overloading "+" can you
please explain a bit more about it ???
thanx.

Jul 23 '05 #9
bool operator ==(MyClass* lhs, MyClass* rhs)

sir, if u could actually tell me why this is not valid....i would actually
like to know when this sort of thing is valid as in something like

bool operator == (something1 , something2)
unless these are static and part of the class or structure as the case
maybe.(as the left side object calls the function by using the this
pointer.)

and when can an operator be overloaded ??

thanking you,
maadhuu.

Jul 23 '05 #10
maadhuu wrote:
bool operator ==(MyClass* lhs, MyClass* rhs)

sir, if u could actually tell me why this is not valid....
This is not valid because you're not allowed to define your own operators
if no argument is of a class or enumeration type. Both arguments here are
pointers. Do you understand the difference between an object being of
a class type and an object being of a pointer to a class type? If you do
not, you need to understand it before you proceed.
i would actually
like to know when this sort of thing is valid as in something like
bool operator == (something1 , something2)
This would be valid if 'something1' _or_ 'something2' have a class type or
an enumeration type.
unless these are static and part of the class or structure as the case
maybe.(as the left side object calls the function by using the this
pointer.)
Yes, if it's a member of a class, then one of the arguments is of that
class type.
and when can an operator be overloaded ??


How many times do I need to repeat the same thing? Get a good book and
read it, will you?

V
Jul 23 '05 #11

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

Similar topics

20
37351
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...
7
4542
by: bberu | last post by:
Hi, I know it is possible to refine operators (like +, -, * or /) on classes. But is it possible on simple types ? ex : If I have a type like : typedef int vector is it possible to redefine a...
1
2208
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
0
1709
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..
1
9984
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type...
3
1141
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
2404
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...
3
1731
by: iluvatar | last post by:
Hi all. I have written a 3d-vector class (for 3-dimensional space) and I have overloaded the arihtmetic operators like +, +=, * and so on. Also, the constructor works with doubles and has...
4
2330
by: Zytan | last post by:
http://www.devarticles.com/c/a/C-Sharp/Making-Your-Code-CLS-Compliant/ Does anyone follow this? I know it's good to not have to deal with unsigned types in the interface, so that's great. But...
0
7260
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,...
0
7161
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
7384
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,...
1
7101
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...
1
5089
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...
0
3234
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...
0
1596
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 ...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.