這裏是解釋的純函數
的概念的示例
#include <iostream>
struct A
{
virtual ~A() = default;
virtual void what() const = 0;
};
void A::what() const
{
std::cout << "struct A";
}
struct B : A
{
virtual void what() const = 0;
};
void B::what() const
{
A::what();
std::cout << ", struct B : A";
}
struct C : B
{
void what() const;
};
void C::what() const
{
B::what();
std::cout << ", struct C: B";
}
int main()
{
// A a; compiler error
// B b; compiler error
C c;
const A &rc = c;
rc.what();
std::cout << std::endl;
return 0;
}
程序輸出是
struct A, struct B : A, struct C: B
在這個例子中的類A和B是抽象的,因爲它們具有純虛函數雖然它們中的每一個都提供了其純虛函數的相應定義。
只有C類不抽象,因爲它將虛函數重新聲明爲非純虛函數。
我認爲你正在尋找的單詞是「_override_」。 – rodrigo
如果你聲明'''void pure()= 0''',那麼你不能在該類中的任何地方定義''void pure(){...}'',並且必須定義void' ){...}'''在任何子類中(如果你不這樣做,子類也被認爲是抽象的)。 –
@Commander香菜蠑螈你錯了。純虛函數可能有一個定義。 –