Overload == for string class

This is a discussion on Overload == for string class within the C++ Programming forums, part of the General Programming Boards category; Howdy, I'm sick of !strcmpi(). I want another function that can tell me if two strings match. In fact, I'd ...

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Overload == for string class

    Howdy,

    I'm sick of !strcmpi(). I want another function that can tell me if two strings match. In fact, I'd like to use an == operator on the strings to see if they match. I remember when I used to use borland C++ Builder, and had access to AnsiString. Those were the days!

    I figured I'll do it myself. I'll overload the == operator for a string class, and return true or false depending on whether the strings matched. This is what I wrote:
    Code:
    class PaulString : public std::string
    {
      public:
        bool operator==(PaulString rhs)
        {
          if (!strcmpi(this, rhs) )
            return true;
          else return false;
        }
    }
    That all seemed fine, except now it wont let me use something like:
    PaulString Name = "Fred";
    because theres no = operator for it. I thought that by having inherited everything from 'std::string' that I would have gotten the = operator.

    If anyone can show me how this should be written (or if its possible), or show me how I can use AnsiString on MSVC6, I would be very thankful

  2. #2
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Make a constructor...

  3. #3
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    I don't get this. Again ;(


    If you use the string class, you already have access to the == operator, at least according to this C++ Reference.

    If you want to compare a real string (class) with those zero terminated char arrays, you probably want to do something like

    if (sMyString == string("Hello Girls!")) [...]

    Not?
    [code]

    your code here....

    [/code]

  4. #4
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    I don't understand neither why he does not want to use the STL string instead of inheriting from it. But, the problem is that he needs a constructor, that's all.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Since strcmpi is used (I think) to perform case insensitive comparisons, it is true that you cannot use the built in == operator for that purpose.

    But your solution of deriving a class from std::string is not a good one. First, the standard string class is not meant to be derived from - it has no virtual methods (and therefore no virtual destructor). I'm not saying you can't do it, but I think the intent of the creators of that class was to not have it be derived from.

    I believe the preferred way to use std::string like that is to make it a private member and then create wrapper methods for each method you want to use. Personally, that seems like an awful lot of work, so I'd suggest simply using strcmpi (or even better _stricmp) or making a global function like this:
    Code:
    bool CaseInsensitiveCompare(const std::string& str1, const std::string& str2)
    {
        return !strcmpi(str1.c_str(), str2.c_str());
    }
    [edit] My code was "bad" so I changed it.
    Last edited by jlou; 10-02-2003 at 11:02 AM.

  6. #6
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Well, the string class' real purpose isn't to serve as a base. But the fact that it has no virtual methods isn't preventing it from being derived, just from being used in polymorphism stuffs.

    [edit]Oh, and your code is bad, there's an operator to do the logical negation...
    Last edited by lyx; 10-02-2003 at 10:58 AM.

  7. #7
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    lol.. I feel so stupid... I never knew the std:string had an == operator. Thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe!
    By Sinensis in forum C Programming
    Replies: 2
    Last Post: 10-21-2008, 04:40 PM
  2. Need help with my code
    By brietje698 in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2007, 02:54 PM
  3. Tic Tac Toe program...
    By Kross7 in forum C++ Programming
    Replies: 12
    Last Post: 04-12-2007, 03:25 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM

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