java 虚函数和纯虚函数有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4823212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between a virtual function and a pure virtual function?
提问by Sairam Lavu
Possible Duplicate:
C++ Virtual/Pure Virtual Explained
可能的重复:
C++ 虚拟/纯虚拟解释
What is the difference between a virtual function and a pure virtual function?
虚函数和纯虚函数有什么区别?
回答by Jerry Coffin
A virtual function canbe overridden in a derived class.
A pure virtual function mustbe overridden in a derived class. Specifically, you can't instantiate a class with a pure virtual function unless at least one derived class overrides that virtual function.
可以在派生类中覆盖虚函数。必须在派生类中覆盖
纯虚函数。具体来说,您不能用纯虚函数实例化一个类,除非至少有一个派生类覆盖了该虚函数。
回答by Jacob Relkin
From Wikipedia:
来自维基百科:
A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class that is not abstract. Classes containing pure virtual methods are termed "abstract;" they cannot be instantiated directly, and a subclass of an abstract class can only be instantiated directly if all inherited pure virtual methods have been implemented by that class or a parent class. Pure virtual methods typically have a declaration (signature) and no definition (implementation).
纯虚函数或纯虚方法是需要由非抽象派生类实现的虚函数。包含纯虚方法的类被称为“抽象”;它们不能直接实例化,抽象类的子类只有在所有继承的纯虚方法都已被该类或父类实现的情况下才能直接实例化。纯虚方法通常具有声明(签名)而没有定义(实现)。
回答by Matteo Italia
A pure virtual function is a virtual function that must be redefined by derived classes, thus usually for it there's no implementation in the base class (although it can be provided, to be called by derived classes via the scope-resolution operator; thanks to @Mark Ransom and @Toolbox for pointing it out).
纯虚函数是一个必须由派生类重新定义的虚函数,因此通常在基类中没有实现(尽管可以提供它,派生类通过范围解析运算符调用;感谢@ Mark Ransom 和@Toolbox 指出了这一点)。
If a class have pure virtual methods it cannot be instantiated, thus any class derived from an abstract class (=a class with pure virtual methods), to be instantiable, must actually define such method.
如果一个类具有纯虚方法,则它不能被实例化,因此任何从抽象类(=具有纯虚方法的类)派生的类,要可实例化,必须实际定义这样的方法。
Example:
例子:
#include <iostream>
using std::cout;
using std::endl;
class BaseClass
{
public:
// A "normal" virtual function which provides a default implementation
virtual void OnlyVirtual()
{
cout<<"BaseClass::OnlyVirtual"<<endl;
}
// A "normal" pure virtual function: must be redefined, no default
// implementation
virtual void PureVirtual()=0;
// A pure virtual function that provides a default implementation that can
// be called only explicitly via scope-resolution operator
virtual void PureVirtualWImpl()=0;
};
void BaseClass::PureVirtualWImpl()
{
cout<<"BaseClass::PureVirtualWImpl"<<endl;
}
class Derived0 : public BaseClass
{
public:
// Define the pure virtual function
virtual void PureVirtual()
{
cout<<"Derived0::PureVirtual"<<endl;
}
// notice that, even if there's an implementation of PureVirtualWImpl in
// BaseClass, since it's marked as pure, Derived0 cannot still be
// instantiated
};
class Derived1 : public Derived0
{
public:
// PureVirtual is already defined by the parent class Derived0
// I must define also PureVirtualWImpl if I want to instantiate this class
virtual void PureVirtualWImpl()
{
cout<<"Derived1::PureVirtualWImpl"<<endl;
}
};
class Derived2 : public Derived1
{
public:
// Obviously I can redefine the "normal" virtual function
virtual void OnlyVirtual()
{
cout<<"Derived2::OnlyVirtual"<<endl;
}
// Redefine PureVirtual
virtual void PureVirtual()
{
cout<<"Derived2::PureVirtual"<<endl;
}
// Just for fun I can redefine PureVirtualWImpl to call the base normally-
// unaccessible implementation
virtual void PureVirtualWImpl()
{
BaseClass::PureVirtualWImpl();
}
};
void TestClass(BaseClass & C)
{
C.OnlyVirtual();
C.PureVirtual();
C.PureVirtualWImpl();
}
int main()
{
// BaseClass b; // <--- compilation error
// Derived0 d0; // <--- compilation error
Derived1 d1;
Derived2 d2;
TestClass(d1);
TestClass(d2);
}
Output:
输出:
BaseClass::OnlyVirtual
Derived0::PureVirtual
Derived1::PureVirtualWImpl
Derived2::OnlyVirtual
Derived2::PureVirtual
BaseClass::PureVirtualWImpl