如何使用父类的运算符? [复制]

Posted

技术标签:

【中文标题】如何使用父类的运算符? [复制]【英文标题】:how to use the operator of parent class? [duplicate] 【发布时间】:2012-04-21 17:20:39 【问题描述】:

可能重复:How to use base class's constructors and assignment operator in C++?

class A

protected:
    void f();


class B : public A

protected:
    void f()
    
         A::f();
    

我们可以这样使用父类的功能,但是我不知道如何使用父类的操作符。

【问题讨论】:

(我猜这是 C++;如果我猜错了,请通过编辑您的问题并放置适当的语言标签来纠正。) ***.com/questions/1226634/… 有一些在 C++ 中从父类调用运算符的示例 【参考方案1】:

用户定义类型的运算符只是具有时髦名称的成员函数。因此,它与您的示例非常相似:

#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;

【讨论】:

以上是关于如何使用父类的运算符? [复制]的主要内容,如果未能解决你的问题,请参考以下文章