琴长不过时光 发表于 2020-3-28 15:25:54

小白求助 重载==操作符的疑问

bool MyString::operator==(const char* p)
{
        if (strcmp(this->m_space, p) == 0)
        {
                return true;
        }
        else
        {
                return false;
        }
}


        if (s2 == "abcdefg")
        {
                cout << "相同" << endl;
        }
        else
        {
                cout << "不相同" << endl;

        }


如题 重载== 操作符,判断一个存储字符串的变量s2,和另外一个字符串是否相同,直接用strcmp行吗 还是要
像下面这样判断
bool MyString::operator==(const char *p) const
{
        if (p == NULL)
        {
                if (m_len == 0)
                {
                        return true;
                }
                else
                {
                        return false;
                }
        }
        else
        {
                if (m_len == strlen(p))
                {
                        return !strcmp(m_p, p);
                }
                else
                {
                        return false;
                }
        }
}

BngThea 发表于 2020-3-28 15:30:03

看你的需求,还有类本身的设计

琴长不过时光 发表于 2020-3-28 15:32:52

#include "MyString.h"

MyString::MyString()
{
        this->m_len = 0;
        this->m_space = new char;
        strcpy(this->m_space, "");
}

MyString::MyString(const char* p)
{
        if (p == NULL)
        {
                this->m_len = 0;
                this->m_space = new char;
                strcpy(this->m_space, "");
        }
        else
        {
                this->m_len = strlen(p);
                this->m_space = new char;
                strcpy(this->m_space, p);
        }
}

MyString::MyString(const MyString& obj)
{
        this->m_len = obj.m_len;
        this->m_space = new char;
        strcpy(this->m_space, obj.m_space);
}

MyString::~MyString()
{
        if (this->m_space != NULL)
        {
                delete[] this->m_space;
                this->m_space = NULL;
                this->m_len = 0;
        }
}

MyString& MyString::operator=(MyString& s)
{
        if (this->m_space != NULL)
        {
                delete[] this->m_space;
                this->m_len = 0;
        }
        this->m_len = s.m_len;
        this->m_space = new char;
        strcpy(this->m_space, s.m_space);
        return *this;
}

MyString& MyString::operator=(const char* p)
{
        if (this->m_space != NULL)
        {
                delete[] this->m_space;
                this->m_len = 0;
        }
        if (p == NULL)
        {
                this->m_len = 0;
                this->m_space = new char;
                strcpy(this->m_space, "");
        }
        else
        {
                this->m_len = strlen(p);
                this->m_space = new char;
                strcpy(this->m_space, p);
        }
        return *this;
}

char& MyString::operator[](int index)
{
        return this->m_space;
}

bool MyString::operator==(MyString& s)
{
        if (this->m_len != s.m_len)
        {
                return false;
        }

        if (strcmp(this->m_space, s.m_space) == 0)
        {
                return true;
        }
        else
        {
                return false;
        }
}

bool MyString::operator==(const char* p)
{
        if (strcmp(this->m_space, p) == 0)
        {
                return true;
        }
        else
        {
                return false;
        }
}

bool MyString::operator!=(MyString& s)
{
        if (!(*this == s))
        {
                return true;
        }
        else
        {
                return false;
        }
}

bool MyString::operator!=(const char* p)
{
        if (!(*this == p))
        {
                return true;
        }
        else
        {
                return false;
        }

}

琴长不过时光 发表于 2020-3-28 15:33:57

BngThea 发表于 2020-3-28 15:30
看你的需求,还有类本身的设计

下面发了 我的MyString.app 你给看下能不能直接用strcmp还是用手动的判断

BngThea 发表于 2020-3-28 15:37:31

琴长不过时光 发表于 2020-3-28 15:33
下面发了 我的MyString.app 你给看下能不能直接用strcmp还是用手动的判断

没其他需求就可以

琴长不过时光 发表于 2020-3-28 19:55:30

BngThea 发表于 2020-3-28 15:37
没其他需求就可以

哦哦 谢谢大佬
页: [1]
查看完整版本: 小白求助 重载==操作符的疑问