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

which are the operators that can be overloaded ??

P: n/a
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
Share this Question
Share on Google+
10 Replies


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

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

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

P: n/a

"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

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

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

P: n/a

"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

P: n/a
i did not quite understand the above argument about overloading "+" can you
please explain a bit more about it ???
thanx.

Jul 23 '05 #9

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.