What is a pure virtual function?

转载 2007年09月14日 16:06:00

What is a pure virtual function?

A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be "pure" using the curious "=0" syntax. For example:
	class Base {
public:
void f1(); // not virtual
virtual void f2(); // virtual, not pure
virtual void f3() = 0; // pure virtual
};

Base b; // error: pure virtual f3 not overridden
Here, Base is an abstract class (because it has a pure virtual function), so no objects of class Base can be directly created: Base is (explicitly) meant to be a base class. For example:
	class Derived : public Base {
// no f1: fine
// no f2: fine, we inherit Base::f2
void f3();
};

Derived d; // ok: Derived::f3 overrides Base::f3
Abstract classes are immensely useful for defining interfaces. In fact, a class with only pure virtual functions is often called an interface.

You can define a pure virtual function:

	Base::f3() { /* ... */ }
This is very occasionally useful (to provide some simple common implementation detail for derived classes), but Base::f3() must still be overridden in some derived class.

If you don't override a pure virtual function in a derived class, that derived class becomes abstract:

	class D2 : public Base {
// no f1: fine
// no f2: fine, we inherit Base::f2
// no f3: fine, but D2 is therefore still abstract
};

D2 d; // error: pure virtual Base::f3 not overridden
 
转载自:http://www.research.att.com/~bs/bs_faq2.html#pure-virtual
                之 
Bjarne Stroustrup's C++ Style and Technique FAQ

相关文章推荐

R6025 pure virtual function call

  • 2010-04-13 11:03
  • 4.51MB
  • 下载

C++ pure virtual function call

最近在写代码的时候,遇到“pure virtual function call”的报错: 问题的背景是这样的,Thread是一个线程的基类,Thread的代码如下: #ifndef _THREAD...

【转载】一篇详尽分析C++“Pure Virtual Function Called”错误的文章

原文链接:http://www.artima.com/cppsource/pure_virtual.html   The C++ Source "Pure Virtual Function Ca...

pure virtual function called--虚函数调用错误

参考:http://www.artima.com/cppsource/pure_virtual.html 前几天我们项目刚刚解决了一个pure virtual function call...

C++读书笔记之纯虚函数pure virtual function

In object-oriented programming, a virtual function or virtual method is a function or method whose b...

pure virtual function called--虚函数调用错误

最近一个项目自己设计了结构,练习使用了一下虚函数,在使用虚函数的时候就遇到了崩溃的问题,在网上查了一下,别人总结的,一起学习一下。。 一、理论上case 当一个纯虚函数被调用到时,vc++...

Something about virtual function and pure virtual function

Firstly, let's demonstrate a fact that virtual function must be implemented. class A { public: A...

虚函数(Virtual Function)与 纯虚函数(Pure Virtual Function)

1>虚函数(Virtual Function) 1.1>Base Class #ifndef Animal_h #define Animal_h #include class Animal { p...

Return to the Basic - 纯虚函数(pure virtual)和抽象类(abstract class)

纯虚函数(pure virtual function)是指在基类中声明但是没有定义的虚函数。 通用形式: virtual type func-name(parameter-list)=0; ...

什么叫pure function(纯函数)

在Knockout中,用到了pureComputer(),其原理来自于纯函数(pure function)。那么,什么叫纯函数呢? 纯函数 (来自:http://en.wikipedia.org/...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)