2012-04-09 85 views
3

可能重复:
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(); 
    } 
} 

我们可以用父类的功能,以这种方式,但我不知道如何使用父类的运营商。

+0

(我猜这是C++,请大家指正,如果我猜错了编辑你的问题,并把相应的语言标记。) – Mat 2012-04-09 09:43:16

+0

http://stackoverflow.com/ questions/1226634/how-to-use-base-classs-constructors-and-assignment-operator-in-c有一些从C++父类中调用操作符的例子 – Mat 2012-04-09 09:44:01

回答

6

用户定义类型的运算符只是具有时髦名称的成员函数。因此,它会很类似的例子:

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