简体   繁体   中英

Logical Comparison == operator overload

I need to do some logical comparison and return a boolean answer.

Here is the code from the .cpp file:

bool MyString::operator==(const MyString& other)const
{
    if(other.Size == this.Size)
    {
            for(int i = 0; i < this.Size+1; i++)
            {
                    if(this[i] == other[i])

                            return true;
            }
    }
    else
            return false;
}

Here is what is called from main.cpp file:

 if (String1 == String4)
 {
    String3.Print ();
 }
 else
 {
     String4.Print ();
 }

Here are there compiling errors I get:

error: request for member `Size` in `this`, which is of non-class type `const MyString* const`
error: no match for `operator[]` in `other[i]`

this is a pointer, hence you have to dereference it:

this->Size;

Also I think that logic of your operator== is flawed - here, it returns true if any of characters is equal to character on same position in second string. Change your loop to

        for(int i = 0; i < this->Size+1; i++)
        {
                if(this[i] != other[i])

                        return false;
        }

and put return true; instead of last part of your code ( else clause) to compare entire strings.

As Seth mentioned, you can't use operator[] on this as above - this way it's treated as array (ie this[i] is really *(this + i) - so not what's you are thinking it is). Access your internal storage member instead.

Problems with your code:

  • this[i] : You apparently want to access the ith character of the string here. This isn't doing that. Assuming your class overloads operator[] , you want (*this)[i] . Alternatively, you could directly access the internal representation of the string.

  • if(this[i] == other[i]) return true; : Think about what this means with respect to comparing the strings "A1" and "AB".

  • for () {...} : What happens when you exit the loop? You need to return something if the comparisons manage to make it through the loop without returning.

You haven't specified if you can use the C++ standard algorithms or not. Here you have illustrated both versions, using hand-written loop and std::equal algorithm:

//#define USE_STD_ALGORITHM 1 // uncomment to enable std::equal version
#include <cassert>
#include <algorithm>
#include <stdexcept>

// NOTE: partial simplest definition for the test and presentation purposes only.
struct MyString
{
    MyString(char const* s, std::size_t size) : data(s), Size(size) {}
    char const& operator[](std::size_t index) const;
    bool operator==(const MyString& other) const;
private:
    char const* data;
    std::size_t Size;
};

char const& MyString::operator[](std::size_t index) const
{
    if (index < Size)
        return data[index];
    throw std::out_of_range("index invalid");
}

bool MyString::operator==(const MyString& other) const
{
    if (this->Size == other.Size)
    {
#ifdef  USE_STD_ALGORITHM
        return std::equal(data, data+Size, other.data);
#else
    bool equal = true;
    for(std::size_t i = 0; i < this->Size; ++i)
    {
        if((*this)[i] != other[i])
        {
            equal = false;
            break;
        }
    }
    return equal;
#endif
    }

    return false;
}

int main()
{
    char const* a = "abc";
    char const* b = "abc";
    MyString sa(a, 3);
    MyString sb(b, 3);
    assert(sa == sb);

    char const* c = "adc";
    MyString sc(c, 3);
    assert(!(sa == sc));

    char const* d = "ab";
    MyString sd(d, 2);
    assert(!(sa == sd));
}

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM