c++ - 修改虚函数中的基类

标签 c++ c++11

我有如下代码,在派生类的虚函数中,如何调用基类中的同一个函数来修改基类?

class Base{
 public:
   int a;
   virtual Base & operator +=(Base const & rhs)
   { 
     a += rhs.a; 
     return *this;
   }
};

class Derived: public Base{
 public:
   int b;
   virtual Derived & operator +=(Derived const & rhs)
   { 
     // What should I write to invoke the += in Base class?
     // something like Base::+=(rhs.Base);
     b += rhs.b;
     return *this;
   }
};

最佳答案

您可以添加:

Base::operator+=(rhs);

调用基本版本。

关于c++ - 修改虚函数中的基类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42616467/

相关文章:

c++ - 在 C++/C 中通配,在 Windows 上

c++ - 从 C++ 执行命令,argv[0] 中预期的是什么?

c++ - 在 C++ 中将短符号签名为字节

android - 无效的 JNI 引用(从 C++ 调用 Java 方法)

c++ - move ctor和 move dtor

c++ - std::move 给出了将 std::string 移动到另一个线程的错误

java - 在 java 的 jni 中使用非静态对象

c++ - C++11标准§12.6.2/2中单词 "constructor' s class"的解释

c++ - 如何编写具有不同数量的信息参数的 C++ 断言宏?

c++ - 递归 unordered_map