Skip to main content
gamedev.net
🔒 Locked

Operator Overloading

Started by Slaru 1 reply 650+ views
Original Post
Slaru
Slaru
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
Reidonius
Reidonius
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]
Slaru
Slaru
Thanks
Wow, that was really really quick. Great Community.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.