Jump to content
  • Advertisement

Archived

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

Yratelev

Basic Operator Overloading

This topic is 5587 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
Advertisement
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
Sneftel:
Are there other posibilities?

operator int() ????
operator char() ???

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!