How to overload operator==() for a pointer to the class?

后端 未结 5 838
北海茫月
北海茫月 2020-12-06 18:06

I have a class called AString. It is pretty basic:

class AString
{
public:
    AString(const char *pSetString = NULL);
    ~AString();
    bool          


        
5条回答
  •  既然无缘
    2020-12-06 18:41

    [ Original answer was wrong and thus corrected below ]

    As pointed out by Oli Charlesworth, in a comment below, this is impossible.

    You would need to define an operator like

       bool operator==(const AString *as, const char *cs); // Note: C++ will not do that
    

    but you cannot overload an operator unless one of its parameters is non-primitive type - and pointers (both pointers to AString and pointers to char) are primitive types.

提交回复
热议问题