Search code examples
c++classoperator-overloadingoperatorsequals-operator

C++: Remove == or any other operator for specific types


If I have a class called classA that has a typecast overload of classB and classC, how can I disable the == operator for classA being typed as classC?

class classA{
   operator classB();
   operator classC();

   //Remove this comparison possibility:
   operator ==(classC firstObject,classC secondObject);
}

Follow over from this question:

Ambiguous overload when using many typecasts operator overloads


Solution

  • In C++11, you can delete methods.
    This is similar to making the method private.
    However, it expresses the programmer's intent and results in more descriptive compiler errors.

    class classA{
       operator classB();
       operator classC();
    
       //Remove this comparison possibility:
       classC operator ==(classC secondObject) = delete;
    };