0

What operator is this code overloading? It doesn't look like the right syntax for the () operator.

class Example
{
public:
  operator bool() const;
...
};

It's used to simulate a boolean member variable, like this:

class Container
{
  public:
    Example ex;
}

void func()
{
  Container c;

  if (c.ex)
  {
  ...
  }
}

Note that ex is used without the parentheses you'd expect from overloading the () operator.

T Scherer
  • 401
  • 7
  • 14

2 Answers2

4

This is operator bool, the implicit conversion operator which allows your class to be used in a boolean context (like if).

jtbandes
  • 115,675
  • 35
  • 233
  • 266
2

That is a user-defined conversion.
It defines a user-defined conversion function that participates in all implicit and explicit conversions.

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271