相关疑难解决方法(0)

如何避免C++中operator ==实现中的错误?

我经常有一些类提供简单的逐个成员比较:

class ApplicationSettings
{
public:
   bool operator==(const ApplicationSettings& other) const;
   bool operator!=(const ApplicationSettings& other) const;

private:
   SkinType m_ApplicationSkin;
   UpdateCheckInterval m_IntervalForUpdateChecks;
   bool m_bDockSelectionWidget;
   // Add future members to operator==
};

bool ApplicationSettings::operator==(const ApplicationSettings& other) const
{
   if (m_ApplicationSkin != other.m_ApplicationSkin)
   {
      return false;
   }

   if (m_IntervalForUpdateChecks != other.m_IntervalForUpdateChecks)
   {
      return false;
   }

   if (m_bDockSelectionWidget != other.m_bDockSelectionWidget)
   {
      return false;
   }

   return true;
}

bool ApplicationSettings::operator!=(const ApplicationSettings& other) const;
{
   return ( ! operator==(other));
}
Run Code Online (Sandbox Code Playgroud)

鉴于此时C++没有提供任何构造来生成运算符==,是否有更好的方法来确保未来成员成为比较的一部分,而不是我在数据成员下面添加的注释?

c++ operators

4
推荐指数
1
解决办法
268
查看次数

标签 统计

c++ ×1

operators ×1