Archived

This topic is now archived and is closed to further replies.

Basic Operator Overloading

This topic is 5303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, this is a real basic question. which operator do i need to overload so that this works: if(THECLASS) Do stuff; class THECLASS { bool DoingStuff; operator (THIS IS THE BIT I NEED HELP ON); } THECLASS::operator (THIS IS THE BIT I NEED HELP ON) { return doing stuff } im sure i just need to know which operator that i overload thanks - Yratelev

Share this post


Link to post
Share on other sites
I think it's a casting operator.

"if" keyword expects a boolean (or maybe an integer) so the correct thing will be a casting to bool operator.


operator bool() {
if(something is true) {
return true;
} else {
return false;
}
}


[edited by - Pepe on June 5, 2003 9:37:12 AM]

Share this post


Link to post
Share on other sites
It''s operator void*.

It would be operator () if you were using a functor like:


if (someInstance()) {
// yay
} else {
// oh no
}


But without the () it''s a different operator.

Share this post


Link to post
Share on other sites
operator void* and operator bool are both acceptable. void* is a bit more historical... it''s used by streams.

Also, you may want to implement operator!.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
quote:
Original post by Yratelev
which operator do i need to overload so that this works:

Unless you genuinely want your class to be implicitly convertible to bool in all contexts, then you should avoid doing it for this one special-case context. Don''t be lazy, write an explicit conversion. It''s both safer and easier to understand.


if(object.is_valid())
do_stuff();

Share this post


Link to post
Share on other sites
I believe the reason they're both acceptable is because the class is cast to a void* in a transition phase before being cast to a bool, so your other operators won't work.

[EDIT] BTW, I agree with SabreMan here.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________


[edited by - Thunder_Hawk on June 5, 2003 12:34:13 PM]

Share this post


Link to post
Share on other sites
Ok looks like Pepe was right. I thought he meant bool operator(). I had no idea there was an operator bool(). You learn something new about C++ every day (damn the language).

So I''m also curious, what other operator XXXX () are there? And how does MSVC handle them? I know it''s notorious for choking on covariant return types.

Share this post


Link to post
Share on other sites
For Dobbs:

You can make a cast operator for every class. So there''s no limit.

About strange operators, I think that most psychotic is:
operator ,

(comma operator)

http://www.gamedev.net/community/forums/topic.asp?topic_id=160439

I think both were right. There are three typical scenarios in C++:

* checking a bool value true.
* checking a scalar value not zero.
* checking a pointer not NULL (zero).

Maybe it would work with every casting operator to a pointer:
char *
String *
...
and with every casting operator to a scalar:
int, char, long, double ....

BTW, I agree with SabreMan too.

Share this post


Link to post
Share on other sites
I like the comma operator in my text class . It''s just implementation after all, so it also supports the standard insertion operator.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

Share this post


Link to post
Share on other sites
Yeah I just found out you could overload comma a few days ago. Strange.

Thanks Pepe, I didn''t know you could overload for bool, int, etc. You think you''re beginning to really know a language and BAM blindsided by something new.

I''m curious what standard behaviour is if you overload multiple casting operators for a class, eg cast to bool and cast to void*, then do "if (someInstance)". Which cast does it use? Boolean seems the logical one (pardon the pun) but either would be acceptable.

Share this post


Link to post
Share on other sites
quote:
Original post by Dobbs
I''m curious what standard behaviour is if you overload multiple casting operators for a class, eg cast to bool and cast to void*, then do "if (someInstance)". Which cast does it use?

There is a standard implicit conversion from any pointer type to bool. Additionally, the compiler will prefer shorter conversion sequences over longer ones (IIRC the compiler will only consider two user-defined conversions in building a conversion sequence). So, `if'' expects a boolean expression, and it will prefer the shorter conversion path of T->bool over T->void*->bool. If a user-defined conversion to bool is not available, then a conversion to any pointer type will be considered, giving the sequence T1->T2*->bool. If there is a conversion to more than one pointer type, then the expression becomes ambiguous, as there is no precedence.

Share this post


Link to post
Share on other sites