简体   繁体   English

派生类的运算符重载

[英]Operator overload for derived classes

I have a base container class, which has a number of operators (=,+,-,+=,etc). 我有一个基本的容器类,它具有许多运算符(=,+,-,+ =等)。 It is expected that the logic of the operators will not need to be changed for the derived classes. 预期对于派生的类,将不需要更改运算符的逻辑。 Thus, ideally, I would like to use the base class operators for all of its derived classes without having to redefine them for each of the derived classes explicitly (with the exception of the assignment operator). 因此,理想情况下,我想对其所有派生类使用基类运算符,而不必为每个派生类显式重新定义它们(赋值运算符除外)。

A solution that I came up with is demonstrated below based on a simple example. 下面基于一个简单示例演示了我提出的解决方案。 The solution seems to work, but I have doubts about the validity of the method for more complicated cases. 该解决方案似乎有效,但是我对这种方法在更复杂的情况下的有效性表示怀疑。 Do you think it is valid to use this assignment "hack" in class B? 您认为在B类中使用此分配“ hack”是否有效? What are the potential pitfalls of this method? 这种方法的潜在陷阱是什么? Is there anything I missed? 我有什么想念的吗? Are there easier ways of achieving the functionality that I need (ie using base class operators for derived classes)? 有没有更简单的方法来实现我需要的功能(即,对派生类使用基类运算符)?

class A
{
 protected:
 int a;
 public:
 A(int ca)
 {
  a=ca;
 }
 A(const A& A1)
 {
  a=A1.a;
 }

 int geta() const
 {
  return a;
 } 
 void seta(int ca)
 {
  a=ca;
 }  
 const A& operator=(const A& A1)
 {
  a=A1.a;
  return *this;
 }

};

const A operator+(const A& A1, const A& A2)
{
 A myA(A1.geta()+A2.geta());
 return myA;
}

class B: public A
{
 public:
 B(int a): A(a) {}// ... using A::A;
 const B& operator=(const B& B1)
 {
  a=B1.geta();
  return *this;
 }
 const B& operator=(const A& B1)
 {
  a=B1.geta();
  return *this;
 }

};

int main()
{

 B myB(4);
 A myA(3);

 //need this to work
 myB=myB+myB;
 cout << myB.geta();
 myA=myA+myA;
 cout << myA.geta();

 int j;
 cin >>  j; 

} }

For the example given i don't see any problems that can happen. 对于给出的示例,我看不到可能发生的任何问题。 Of cause you can improve the code, first by returning non const reference in operator=, second i think by adding += op to your class, and using it's code inside global operator+ function. 当然,您可以改进代码,首先通过在operator =中返回非const引用,其次我想通过在类中添加+ = op并在全局operator +函数中使用它的代码。

But in general i think it should work fine. 但总的来说,我认为它应该可以正常工作。 As for assignment operators - as soon as you have only POD types and no pointers, or references or handles you don't really need any. 至于赋值运算符-只要您只有POD类型,而没有指针,引用或句柄,那么您实际上就不需要了。 It will become more complicated, however when pointers appear. 但是,当出现指针时,它将变得更加复杂。 You'll need to make sure you copy objects, pointed by them, or manage them some other way. 您需要确保复制对象,对象指向的对象或以其他方式对其进行管理。

And you probably will not need to modify your operators as long as you don't add more members to derived classes, which also should take part in calculations. 只要您不向派生类添加更多成员,您就可能不需要修改运算符,派生类也应参与计算。

In general you don't have to redefine functions in the base class for derived classes. 通常,您不必在基类中为派生类重新定义函数。 With public inheritance your derived classes will have access to those functions and use their operation just fine. 通过公共继承,您的派生类将可以访问这些函数并很好地使用它们的操作。 If you do want to re-define them (= operator for example), be sure you call the right one (virtual functions help in this case). 如果确实要重新定义它们(例如=运算符),请确保调用正确的函数(在这种情况下,虚拟函数会有所帮助)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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