Public Group

This topic is 5231 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
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 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.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 30
• 9
• 16
• 22