Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ 'overloading' the if() statement

Is it possible to change the behavior of if() so that:

class Foo {
    int x;
};

Foo foo;
if(foo)

only proceeds if the value of x is something other than zero? or...

Would an explicit user-defined type conversion to int work/would that be an appropriate approach? or...

Is it best to do something like if(foo.getX())?

like image 297
Flexo1515 Avatar asked May 08 '13 02:05

Flexo1515


People also ask

Does overloading == also overload !=?

NO. There is no such requirement that you Must overload != If you need to overload == . However,it is a good practice that you Should overload operators related to each other.

Can the conditional operator be overloaded?

You cannot overload the conditional operator.

What are the rules for overloading?

New operators can not be created. 2) Arity of the operators cannot be changed. 3) Precedence and associativity of the operators cannot be changed. 4) Overloaded operators cannot have default arguments except the function call operator () which can have default arguments.

What is overloading operator in C?

Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type.


2 Answers

You can define an operator to convert the object to bool

class Foo
{
  int x;
public:
  operator bool() const
  {
    return x > 0;
  }
};

But this can have unintended consequences because of implicit conversions to bool when you don't desire the conversion to take place. For instance

int x = 42 + Foo();

C++11 solves this problem by allowing you to declare the conversion operator as explicit, which then only allows implicit conversions in certain contexts, such as within an if statement.

explicit operator bool() const // allowed in C++11

Now

int x = 42 + Foo();  // error, no implicit conversion to bool
int x = 42 + static_cast<bool>(Foo()); // OK, explicit conversion is allowed
like image 173
Praetorian Avatar answered Oct 24 '22 13:10

Praetorian


You can convert your object to a boolean value by defining operator bool():

explicit operator bool() const 
{ 
    return foo.getX(); 
}

The explicit keyword prevents implicit conversions from Foo to bool. For example, if you accidentally put foo in an arithmetic expression like foo + 1, the compiler could detect this error if you declare operator bool() as explicit, otherwise foo will be converted to bool even if not intended.

In general, member functions of the form

operator TypeName()

(with optional explicit and const qualifier) are conversion operators. It allows you to cast your class to any type specified by TypeName. In the other direction, constructors with one argument allow you to cast any type to your class:

class Foo {
  Foo(int x);    // convert int to Foo
  operator bool() const;  // convert Foo to bool
  int x;
};

This defines implicit conversions for your class. The compiler tries to apply these conversions if possible (like what it does for built-in data types, e.g. 5 + 1.0). You can declare them to be explicit to suppress unwanted implicit conversions.

like image 26
Yang Avatar answered Oct 24 '22 14:10

Yang