How can I check whether a certain type implements a certain operator?
struct CustomOperatorsClass
{
public int Value { get; private set; }
public CustomOperatorsClass( int value )
: this()
{
Value = value;
}
static public CustomOperatorsClass operator +(
CustomOperatorsClass a, CustomOperatorsClass b )
{
return new CustomOperatorsClass( a.Value + b.Value );
}
}
Following two checks should return true
:
typeof( CustomOperatorsClass ).HasOperator( Operator.Addition )
typeof( int ).HasOperator( Operator.Addition )
The operator that cannot be overloaded in C language is the Scope resolution operator (::).
You can only overload existing operators. You can't overload new operators. Some operators cannot be overloaded using a friend function. However, such operators can be overloaded using member function.
So operator overloading lets us define the meaning of an existing operator (note that you cannot overload some operators) for the operands of a user defined type (for example, a class is a user defined type).
The two member access operators, operator->() and operator->*() can be overloaded. The most common use of overloading these operators is with defining expression template classes, which is not a common programming technique.
You should check if class have method with op_Addition
name
You can find overloaded method names here,
Hope this helps
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