3 回答
![?](http://img1.sycdn.imooc.com/545867280001ed6402200220-100-100.jpg)
TA贡献1856条经验 获得超11个赞
C ++语法如下:
class Bar : public Foo {
// ...
void printStuff() {
Foo::printStuff(); // calls base class' function
}
};
![?](http://img1.sycdn.imooc.com/54584dc4000118d302200220-100-100.jpg)
TA贡献1868条经验 获得超4个赞
是,
class Bar : public Foo
{
...
void printStuff()
{
Foo::printStuff();
}
};
它与superJava中的相同,不同之处在于它允许您在具有多个继承时从不同的基础调用实现。
class Foo {
public:
virtual void foo() {
...
}
};
class Baz {
public:
virtual void foo() {
...
}
};
class Bar : public Foo, public Baz {
public:
virtual void foo() {
// Choose one, or even call both if you need to.
Foo::foo();
Baz::foo();
}
};
![?](http://img1.sycdn.imooc.com/54585094000184e602200220-100-100.jpg)
TA贡献1854条经验 获得超8个赞
有时,当您不在派生函数中时,您需要调用基类的实现...它仍然有效:
struct Base
{
virtual int Foo()
{
return -1;
}
};
struct Derived : public Base
{
virtual int Foo()
{
return -2;
}
};
int main(int argc, char* argv[])
{
Base *x = new Derived;
ASSERT(-2 == x->Foo());
//syntax is trippy but it works
ASSERT(-1 == x->Base::Foo());
return 0;
}
- 3 回答
- 0 关注
- 492 浏览
添加回答
举报