which are the operators that can be overloaded ??

M

maadhuu

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)
 
V

Victor Bazarov

maadhuu said:
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
 
V

Victor Bazarov

Aaron said:
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!
if u overload . how will you access class members?




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
 
H

Howard

Aaron Gage said:
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::eek:perator ==(const MyClass& rhs)

but not this:

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


-Howard
 
T

tedu

Victor said:
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;
}
 
V

Victor Bazarov

tedu said:
Victor Bazarov wrote:




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
 
H

Howard

tedu said:
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
 
M

maadhuu

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

Howard

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

It might help if you included the specific text that you're asking about,
don't you think? At least some explanation about what point I made that
you're not understanding? Preferably, both.

-Howard
 
M

maadhuu

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.
 
A

Aaron Gage

Victor said:
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?


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 .*
 
V

Victor Bazarov

maadhuu said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top