• Advertisement

Archived

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

Operator Overloading

This topic is 5043 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
Thanks
Wow, that was really really quick. Great Community.

Share this post


Link to post
Share on other sites

  • Advertisement