在使用继承时实现operator == [英] implementing operator== when using inheritance

查看:88
本文介绍了在使用继承时实现operator ==的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现==运算符的基类。
我想编写另一个类,继承基类,并且应该重新实现==运算符。



下面是一些示例代码:

  #include< iostream> 
#include< string>

class Person
{
public:
Person(std :: string Name){m_Name =名称; };

bool运算符==(const Person& rPerson)
{
return m_Name == rPerson.m_Name;
}

private:
std :: string m_Name;
};

类雇员:public Person
{
public:
Employee(std :: string Name,int Id):Person(Name){m_Id = Id; };

bool运算符==(const Employee& rEmployee)
{

return(Person :: operator ==(rEmployee))&& (m_Id == rEmployee.m_Id);
}

私人:
int m_Id;
};

void main()
{
Employee * pEmployee1 = new Employee( Foo,1);
Employee * pEmployee2 = new Employee( Foo,2);

if(* pEmployee1 == * pEmployee2)
{
std :: cout<< 相同的雇员;
}
其他
{
std :: cout<< 不同的雇员;
}

人* pPerson1 = pEmployee1;
人* pPerson2 = pEmployee2;

if(* pPerson1 == * pPerson2)
{
std :: cout<< 同一个人;
}
其他
{
std :: cout<< 不同的人;
}
}

此示例代码给出以下结果:

 不同的雇员
同一个人

即使在处理Person *指针时,我也要确保它们不同。



我应该如何解决此问题?



谢谢!

解决方案

您想要做的是必不可少的



由于运算符不能是虚拟的,因此您需要将其委托给其他对象。这是一个可能的解决方案。

  class Person 
{
public:
/ * .. * /
bool运算符==(const Person& rhs)
{
return m_Name == rPerson.m_Name& this-> doCompare(rhs);
}
私人:
虚拟bool doCompare()= 0;
};
}
类雇员:公共人员
{
/ * ... * /
私人:
虚拟布尔doCompare(const Person& rhs)
{
bool bRetval = false;
const Employee * pRHSEmployee = dynamic_cast< const Employee *>(& rhs);
if(pEmployee)
{
bRetval = m_Id == pRHSEmployee-> m_Id
}
return bRetval;
}
};

这个问题并不清楚人是否需要成为一个具体的阶级。如果是这样,您可以使其不是纯虚拟的,并实现它以返回true。



这也使用RTTI,您可能会满意,也可能不满意。 / p>

I have a base class which implements the == operator. I want to write another class, inheriting the base class, and which should reimplement the == operator.

Here is some sample code :

#include <iostream>
#include <string>

class Person
{
public:
  Person(std::string Name) { m_Name = Name; };

  bool operator==(const Person& rPerson)
  {
    return m_Name == rPerson.m_Name;
  }

private:
  std::string m_Name;
};

class Employee : public Person
{
public:
  Employee(std::string Name, int Id) : Person(Name) { m_Id = Id; };

  bool operator==(const Employee& rEmployee)
  {

    return (Person::operator==(rEmployee)) && (m_Id == rEmployee.m_Id);
  }

private:
  int m_Id;
};

void main()
{
  Employee* pEmployee1 = new Employee("Foo" , 1);
  Employee* pEmployee2 = new Employee("Foo" , 2);

  if (*pEmployee1 == *pEmployee2)
  {
    std::cout << "same employee\n";
  }
  else
  {
    std::cout << "different employee\n";
  }

  Person* pPerson1 = pEmployee1;
  Person* pPerson2 = pEmployee2;

  if (*pPerson1 == *pPerson2)
  {
    std::cout << "same person\n";
  }
  else
  {
    std::cout << "different person\n";
  }
}

This sample code give the following result :

different employee
same person

Where I would like, even when handling Person* pointers, to make sure they are different.

How am I supposed to solve this problem ?

Thanks !

解决方案

What you want to do is essentiall "virtualize" the comparison operator.

Since operators cannot be virtual, you will need to delegate it to something else. Here's one possible solution.

class Person
{
   public:
      /* ... */
      bool operator==(const Person& rhs)
      {
         return m_Name == rPerson.m_Name && this->doCompare(rhs);
      }
   private:
      virtual bool doCompare() = 0;
   };
}
class Employee : public Person
{
   /* ... */
   private:
      virtual bool doCompare(const Person& rhs)
      {
         bool bRetval = false;
         const Employee* pRHSEmployee = dynamic_cast<const Employee*>(&rhs);
         if (pEmployee)
         {
            bRetval = m_Id == pRHSEmployee->m_Id
         }
         return bRetval;
      }
};

The question didn't make clear whether Person needs to be a concrete class. If so, you can make it not pure-virtual, and implement it to return true.

This also uses RTTI, which you may or may not be happy with.

这篇关于在使用继承时实现operator ==的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆