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
?
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With