Can anyone please help me out with Program to overload < and > to compare two strings.
Asked
Active
Viewed 475 times
-5
-
Search the web and Stack Overflow using the words "C++ operator overloading example" – Thomas Matthews Oct 21 '13 at 13:01
-
4Strings already have overloaded `<` and `>` operators. – interjay Oct 21 '13 at 13:01
-
*"Can anyone please help me out with Program to overload < and > to compare two strings."* - No. You don't need help, it's already done. – Maroun Oct 21 '13 at 13:02
-
I'd be happy to help if you show me what you've tried so far and what exactly you're having trouble with. But this isn't a tutorial site -- we need you to try to figure it out on your own first. – John Dibling Oct 21 '13 at 13:02
-
1And to add to the previous comments: you cannot legally do it, because you're not allowed to redefine parts of the standard language. – James Kanze Oct 21 '13 at 13:03
1 Answers
0
There are different things that can be called strings in C++, std::string
is the main one, and that already provides those operators. C-style null terminated strings are another, and string literals yet another. For the latter two, you cannot overload operator<
or operator>
, as you can only overload operators for user defined types.
Even if you were allowed to overload the operators, they would misbehave in obscure ways, as the set of associated namespaces for const char*
(or char*
for that matters) is empty, Argument Dependent Lookup would fail to find your overload if it finds a different operator earlier during regular lookup.

David Rodríguez - dribeas
- 204,818
- 23
- 294
- 489
-
Thank you, As i am new to this language could you suggest me book to understand C++ from basic? – Mohsin Oct 21 '13 at 13:27
-
@Mohsin: There is already a [list of recommended books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) in this site. – David Rodríguez - dribeas Oct 21 '13 at 13:59