SNpn
SNpn

Reputation: 2207

String operators C++

I was wondering if you can compare two strings using ==. I have a function which takes in a const value &item and since its a value type there is no way I can determine what type the value is, hence if the value type is a string, == may not work.

Hence the question, what would be the best way to tackle this problem? I was thinking of overloading the == operator, but is there an easy way?

Upvotes: 0

Views: 453

Answers (3)

Juraj Blaho
Juraj Blaho

Reputation: 13451

In C++ operator == for std::string compares the content of the strings.

Upvotes: 4

Nim
Nim

Reputation: 33655

There is already a bunch of operators implemented for std::string (compare std::string and const char* etc.)

If you have a custom type, then you'll need to provide operators for those.

Upvotes: 1

Bo Persson
Bo Persson

Reputation: 92211

If the string is a std::string it already has an operator== defined. It compares the contents of the strings.

If it is a C string (char*) the comparison is a pointer comparison that tells us if the pointers points to the same string. You cannot overload this either as it is a built in operator.

Upvotes: 2

Related Questions