Public Group

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

• 23
• 10
• 19
• 16
• 14