How to Overload the Conditional Operator

How to overload the conditional operator?

Several operators cannot be overloaded. These operators take a name, rather than an object, as their right operand:

  • Direct member access (.)

  • Deference pointer to class member (.*)

  • Scope resolution (::)

  • Size of (sizeof)

The conditional operator (?:) also cannot be overloaded.

Additionally, the new typecast operators: static_cast<>, dynamic_cast<>, reinterpret_cast<>, and const_cast<>, and the # and ## preprocessor tokens cannot be overloaded.

http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=23

Why is it not possible to overload the ternary operator?

I think the main reason at the time that it didn't seem worth
the effort of inventing a new syntax just for that operator.
There is no token ?:, so you'd have to create a number of
special grammar rules just for it. (The current grammar rule
has operator followed by an operator, which is a single
token.)

As we've learned (from experience) to use operator overloading
more reasonably, it has become apparent that we really shouldn't
have allowed overloading of && and || either, for the
reasons other responses have pointed out, and probably not
operator comma as well (since the overloaded versions won't have
the sequence point which the user expects). So the motivation
to support it is even less than it was originally.

How to overload the ternary operator (?:) in C++?

You can't (§13.5/3):

The following operators cannot be overloaded:
. .* :: ?:
nor can the preprocessing symbols
#
and
##
(Clause
16).

Can I overload the conditional operator in C#?

The list of overloadable operators is here. Some you can, and some you can't.

This page shows how to overload the ones that can be overloaded.

As for the conditional operator (?:), no, you cannot overload it. It is specific to booleans. What would it mean if you did overload it? It's a shorthand for if/then/else.

I suppose the way you could rephrase the question is: "Why can't I overload if/then/else"?

The real reason, more than likely, probably has to do with the fact that the designers of the language chose not to implement it. More than likely, providing an overload for this operator doesn't provide enough benefit to merit the testing and effort that would go into making it overloadable. In short, it's probably a time/money reason. For every feature added to a computer language, that means time, effort and testing, which has to be balanced against the benefit provided by the feature.

In the end, some things just aren't worth implementing. While I'm sure you can find some benefit to it, the benefit is probably outweighed by the cost of implementation.

How to overload Null-conditional operators ?.

No, you can't overload the Null-conditional operators. See the list of C# Overloadable operators.


Addendum The ability to overload this operator has actually been proposed to the C# language team. See Proposal: Allow null conditional (?.) and null coalescing ()?? operators to be overloaded and Proposal: nullable-like types. These has not been aproved.

What follows is my understanding of the concerns regarding these and similar proposals:

Changing the semantics of these operators could have a lot of ramifications. For example, given that the operators are static, it would be possible to make it say that something that is null is not. Which would mean a lot of problems for a lot of code. On the flip side, you could have code that hangs too long on a reference or not long enough that would bring problems with the garbage collection.

Even if these and similar issues could be solved, it is not a change to be taken lightly. We are talking about a lot of problems for existing code, that is already deployed in production.

How to use overload operator as condition in a if statment?

A == needs two arguments (even if the overload is a member), you would write the if as any other if statement:

if(circle1 == circle2) { ... }

and if there's a matching overload the compiler would transform that into something like:

if(circle1.operator ==(circle2)) { ... }

How overload the '=' operator with arguments?

When you overload the = operator, you only want to have the right-hand value in the arguments. Since you overloaded the () operator, you don't need to handle the r and c value with the = operator. You can just use mt(2,4) = 3.5; and the overloaded () operator will handle the mt(2,4) portion. Then, you can just set the returned data to your desired value without overloading any = operator.

You need to return a reference to the data so you can edit it, however:

template <class _type>
_type& myClass<_type>::operator()(int r,int c) {
return data[r*nCols+c];
};

Overload ternary ?: operator, or change to if{}else{} in included files

According to the C++ standard you are not permitted to overload ?:
The best you can do is use C macros (but this can lead to horrible code).



Related Topics



Leave a reply



Submit