鱼C论坛

 找回密码
 立即注册
查看: 2158|回复: 5

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

[复制链接]
发表于 2020-3-28 15:25:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. bool MyString::operator==(const char* p)
  2. {
  3.         if (strcmp(this->m_space, p) == 0)
  4.         {
  5.                 return true;
  6.         }
  7.         else
  8.         {
  9.                 return false;
  10.         }
  11. }
复制代码


  1.         if (s2 == "abcdefg")
  2.         {
  3.                 cout << "相同" << endl;
  4.         }
  5.         else
  6.         {
  7.                 cout << "不相同" << endl;

  8.         }
复制代码



如题 重载== 操作符,判断一个存储字符串的变量s2,和另外一个字符串是否相同,直接用strcmp行吗 还是要
像下面这样判断
  1. bool MyString::operator==(const char *p) const
  2. {
  3.         if (p == NULL)
  4.         {
  5.                 if (m_len == 0)
  6.                 {
  7.                         return true;
  8.                 }
  9.                 else
  10.                 {
  11.                         return false;
  12.                 }
  13.         }
  14.         else
  15.         {
  16.                 if (m_len == strlen(p))
  17.                 {
  18.                         return !strcmp(m_p, p);
  19.                 }
  20.                 else
  21.                 {
  22.                         return false;
  23.                 }
  24.         }
  25. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-28 15:30:03 | 显示全部楼层
看你的需求,还有类本身的设计
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-28 15:32:52 | 显示全部楼层
  1. #include "MyString.h"

  2. MyString::MyString()
  3. {
  4.         this->m_len = 0;
  5.         this->m_space = new char[m_len + 1];
  6.         strcpy(this->m_space, "");
  7. }

  8. MyString::MyString(const char* p)
  9. {
  10.         if (p == NULL)
  11.         {
  12.                 this->m_len = 0;
  13.                 this->m_space = new char[m_len + 1];
  14.                 strcpy(this->m_space, "");
  15.         }
  16.         else
  17.         {
  18.                 this->m_len = strlen(p);
  19.                 this->m_space = new char[m_len + 1];
  20.                 strcpy(this->m_space, p);
  21.         }
  22. }

  23. MyString::MyString(const MyString& obj)
  24. {
  25.         this->m_len = obj.m_len;
  26.         this->m_space = new char[m_len + 1];
  27.         strcpy(this->m_space, obj.m_space);
  28. }

  29. MyString::~MyString()
  30. {
  31.         if (this->m_space != NULL)
  32.         {
  33.                 delete[] this->m_space;
  34.                 this->m_space = NULL;
  35.                 this->m_len = 0;
  36.         }
  37. }

  38. MyString& MyString::operator=(MyString& s)
  39. {
  40.         if (this->m_space != NULL)
  41.         {
  42.                 delete[] this->m_space;
  43.                 this->m_len = 0;
  44.         }
  45.         this->m_len = s.m_len;
  46.         this->m_space = new char[m_len];
  47.         strcpy(this->m_space, s.m_space);
  48.         return *this;
  49. }

  50. MyString& MyString::operator=(const char* p)
  51. {
  52.         if (this->m_space != NULL)
  53.         {
  54.                 delete[] this->m_space;
  55.                 this->m_len = 0;
  56.         }
  57.         if (p == NULL)
  58.         {
  59.                 this->m_len = 0;
  60.                 this->m_space = new char[m_len + 1];
  61.                 strcpy(this->m_space, "");
  62.         }
  63.         else
  64.         {
  65.                 this->m_len = strlen(p);
  66.                 this->m_space = new char[m_len + 1];
  67.                 strcpy(this->m_space, p);
  68.         }
  69.         return *this;
  70. }

  71. char& MyString::operator[](int index)
  72. {
  73.         return this->m_space[index];
  74. }

  75. bool MyString::operator==(MyString& s)
  76. {
  77.         if (this->m_len != s.m_len)
  78.         {
  79.                 return false;
  80.         }

  81.         if (strcmp(this->m_space, s.m_space) == 0)
  82.         {
  83.                 return true;
  84.         }
  85.         else
  86.         {
  87.                 return false;
  88.         }
  89. }

  90. bool MyString::operator==(const char* p)
  91. {
  92.         if (strcmp(this->m_space, p) == 0)
  93.         {
  94.                 return true;
  95.         }
  96.         else
  97.         {
  98.                 return false;
  99.         }
  100. }

  101. bool MyString::operator!=(MyString& s)
  102. {
  103.         if (!(*this == s))
  104.         {
  105.                 return true;
  106.         }
  107.         else
  108.         {
  109.                 return false;
  110.         }
  111. }

  112. bool MyString::operator!=(const char* p)
  113. {
  114.         if (!(*this == p))
  115.         {
  116.                 return true;
  117.         }
  118.         else
  119.         {
  120.                 return false;
  121.         }

  122. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-28 15:33:57 | 显示全部楼层
BngThea 发表于 2020-3-28 15:30
看你的需求,还有类本身的设计

下面发了 我的MyString.app 你给看下  能不能直接用strcmp  还是用手动的判断
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

没其他需求就可以
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-28 19:55:30 | 显示全部楼层
BngThea 发表于 2020-3-28 15:37
没其他需求就可以

哦哦 谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-4 09:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表