Click here to Skip to main content
15,608,738 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was learning about operator overloading and stumbled upon the concept of overloading equality and inequality operators. It states that when comparing with two instances of a class, these operators work on simple data types, but do not work when comparing non-static string members(char*). So i have some questions:

1. Why does it work on simple data types, but not on non-static string members?
2. What does it mean for a data type to be non-static?

What I have tried:

I have tried looking for the answer
Updated 18-Aug-21 3:25am

1. Why does it work on simple data types, but not on non-static string members?

Incorrect: Works on anything, but for user defined data types you need to write equality operator. For example if your non static type is char*, you do it like:

class MyClass
   char* myString;

   bool operator == (const MyClass& rhs)
      // check for self
     if(this == &rhs)
       return true;
     if(myString == nullptr && rhs.myString == nullptr)
       return true;
     if(strcmp(myString, rhs.myString) == 0)
       return true;
     return false;

    bool operator != (const MyClass& rhs)
      // Always defined in terms of == operator
      return !(*this == rhs);
Share this answer
1. If you overload these operators then you must write the code to do the comparisons.
2. Non-static data members are part of each object that is created when instantiating the class.

If you want to learn C++ then here are some good sites: C++ language -[^] and Learn C++ – Skill up with our free tutorials[^].
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900