我'll take the risk of stating the obvious: You call the function, if it'在基类中定义它's automatically available in the derived class (unless it' s private ) .
如果派生类中存在具有相同签名的函数,则可以通过添加基类的名称后跟两个冒号 base_class::foo(...) 来消除歧义 . 您应该注意,与Java和C#不同,C确实 not 具有"the base class"( super 或 base )的关键字,因为C支持multiple inheritance,这可能导致歧义 .
class left {
public:
void foo();
};
class right {
public:
void foo();
};
class bottom : public left, public right {
public:
void foo()
{
//base::foo();// ambiguous
left::foo();
right::foo();
// and when foo() is not called for 'this':
bottom b;
b.left::foo(); // calls b.foo() from 'left'
b.right::foo(); // call b.foo() from 'right'
}
};
顺便说一下,你不能直接从同一个类派生两次,因为没有办法引用其中一个基类 .
class bottom : public left, public left { // Illegal
};
624
给定名为 Parent 的父类和名为 Child 的子类,您可以执行以下操作:
class Parent {
public:
void print(int x);
}
class Child : public Parent {
void print(int x) override;
}
void Parent::print(int x) {
// some default behavior
}
void Child::print(int x) {
// use Parent's print method; implicitly passes 'this' to Parent::print
Parent::print(x);
}
6 回答
如果您的基类名为
Base
,并且您的函数名为FooBar()
,则可以使用Base::FooBar()
直接调用它在MSVC中,有一个Microsoft特定的关键字: __super
MSDN:允许您明确声明您正在为要覆盖的函数调用基类实现 .
我'll take the risk of stating the obvious: You call the function, if it'在基类中定义它's automatically available in the derived class (unless it' s
private
) .如果派生类中存在具有相同签名的函数,则可以通过添加基类的名称后跟两个冒号
base_class::foo(...)
来消除歧义 . 您应该注意,与Java和C#不同,C确实 not 具有"the base class"(super
或base
)的关键字,因为C支持multiple inheritance,这可能导致歧义 .顺便说一下,你不能直接从同一个类派生两次,因为没有办法引用其中一个基类 .
给定名为
Parent
的父类和名为Child
的子类,您可以执行以下操作:请注意,
Parent
是类的实际名称,而不是关键字 .如果基类成员函数的访问修饰符受保护或公共,则可以从派生类调用基类的成员函数 . 可以从派生成员函数调用基类非虚拟和虚拟成员函数 . 请参考该计划 .
输出:
参考例子 .