C++ Overloading < > == for use with char * or string

This is a discussion on C++ Overloading < > == for use with char * or string within the C++ Programming forums, part of the General Programming Boards category; I am trying to figure out how to overload <, >, and == to use with char * or string ...

  1. #1
    &operator overload neolyn's Avatar
    Join Date
    Nov 2004
    Location
    Morgantown, WV
    Posts
    16

    Question C++ Overloading < > == for use with char * or string

    I am trying to figure out how to overload <, >, and == to use with char * or string datatypes.

    I am trying to take set of data and put into a data structure by value of the string, so I need a way to calculate:

    dog > cat because D comes after C in the alphabet, and so on.

    Please help - I'm really new to overloaded operators.
    Thanks.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    well if you are using a char * then you can just use strcmp.
    If you are using std::string then it already has built in comparisons

    strcmp(a,b) returns >1 if a > b, < 1 if a < b, or 0 if a==b.
    Generally i only write two comparison operators and then use those two for the other four
    Code:
    bool operator == (const char *a, const char *b)
    {
      return strcmp(a,b) == 0;
    }
    
    bool operator > (const char *a, const char *b)
    {
      return strcmp(a,b) > 1;
    }
    
    bool operator >= (const char *a, const char *b)
    {
      return (a==b) || (a>b);
    }
    Warning: I have not compiled or tested the above code and am not 100% sure it is correct, or if you can even overload the comparison between to char * in such a manner.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Err... I don't believe you can overload operations on built in data types.
    You could make a thin structure over them and then add your operators. You could also use std::string in <string> as it already has those operators.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Error message in tic tac toe problem please help
    By Kross7 in forum C++ Programming
    Replies: 17
    Last Post: 04-10-2007, 01:50 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21