NindzAI
NindzAI

Reputation: 580

Ran into this at work "operator ClassName *". What does this mean?

The class with this code is a reference class for a pointer of ClassName, i.e.:

class ClassName;

class ClassRef
{
    ClassName* m_class;
    ...
    operator ClassName *() const { return m_class; }
...

I am assuming this is used for pointer validity checks, such as:

ClassRef ref(new ClassName())
if (ref) { bla bla bla }

Am I correct in my thinking?

Upvotes: 5

Views: 2057

Answers (1)

enobayram
enobayram

Reputation: 4698

This is an overload of the conversion operator. Whenever a ClassRef object needs to be converted to a ClassName pointer type, this operator is called.

So;

ClassRef r;
ClassName * p = r;

will make use of this overload.

Upvotes: 10

Related Questions