Jump to content
  • Advertisement

Archived

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

Slaru

Operator Overloading

This topic is 5220 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, I am trying to overload operators in a class of mine. I have overloaded the ! operator, but say i were to say: template class foo { ... private: T* obj; }; foo yum; if (yum) {...} Is there a way i can overload the ''if (object)'' operator (is that even an operator?) so that it checks the T* obj for validity? I hope you understand my question. If you don''t, could you please point me to a great resource on operator overloading? Slaru

Share this post


Link to post
Share on other sites
Advertisement
Yup. You overload operator bool(), like so:

void foo::operator bool() const
{
return (somebooleanexpressionofyourchoosing);
}

And then in your program, you can say

foo myFoo;

// Do useful things

if (myFoo) {
// Do more useful things;
}

[edited by - Reidonius on May 6, 2004 11:56:33 PM]

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!