Gim*_*Eom 4 c++ inheritance operators
可能重复:
如何在C++中使用基类的构造函数和赋值运算符?
class A
{
protected:
void f();
}
class B : public A
{
protected:
void f()
{
A::f();
}
}
Run Code Online (Sandbox Code Playgroud)
我们可以用这种方式使用父类的功能,但我不知道如何使用父类的运算符.
用户定义类型的运算符只是具有时髦名称的成员函数.所以,它与你的例子非常相似:
#include <iostream>
class A
{
protected:
A& operator++() { std::cout << "++A\n"; return *this; }
};
class B : public A
{
public:
B& operator++()
{
A::operator++();
return *this;
}
};
int main()
{
B b;
++b;
}
Run Code Online (Sandbox Code Playgroud)