Jump to content
  • Advertisement
Sign in to follow this  
Chris81

Help understanding code snippet - operator overloading

This topic is 4721 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

Hello, I understand how operator overloading works in C++. I've written classes that have made use of overloading +, -, ->, etc. However I came across the following which left me confused:
template <class T>
class Foo
{
   ...
   operator T* () const;
   ...
private:
   Object *Bar;
}

...

template <class T>
Foo<T>::operator T* () const
{
   return Bar;
}


How does this operator work, what is this for? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
It's a conversion operator, it will use that operator when an cast (implicit or explicit) is made to convert Foo< T > to T*. So when you do something like:

Object* bar = Foo< Object >();



It will call operator T* to do the conversion.

Share this post


Link to post
Share on other sites
Okay, thanks. I was guessing it had to do with casting, but wasn't sure. The descriptions I read for operator overloading didn't mention this.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!