Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary operator and function signature

Let's say I have a C++ class with two functions like

class MyClass
{
    bool Foo(int val);
    bool Foo(string val);
}

Is it possible to use the ternary operator like this

MyClassInstance->Foo(booleanValue?24:"a string");

and have a different function of MyClass invoked depending on the value of booleanValue?

like image 236
Yannick Blondeau Avatar asked Oct 24 '12 13:10

Yannick Blondeau


1 Answers

Not with the ternary operator. The type of a ternary expression is the common type of its second and third operands; if they have no common type you can't use it. So just use an ordinary if statement:

if (booleanValue)
    MyClassInstance->Foo(24);
else
    MyClassInstance->Foo("a string");
like image 112
Pete Becker Avatar answered Sep 22 '22 05:09

Pete Becker