Sie sind auf Seite 1von 2

c++ - Difference between a virtual function and a pure virtual function ...

1 of 2

http://stackoverflow.com/questions/2652198/difference-between-a-virt...

Difference between a virtual function and a pure virtual function [duplicate]

Possible Duplicate:
C++ Virtual/Pure Virtual Explained
Hi,
Need to know what is the difference between a pure virtual function and a virtual function?
I know "Pure Virtual Function is a Virtual function with no body" but what does this mean and what is actually
done by the line below
virtual void virtualfunctioname() = 0
c++

function

virtual

pure-virtual

edited Mar 14 at 3:14


Richard JP Le Guen
9,352 1 21 57

asked Apr 16 '10 at 10:33


Sachin Chourasiya
1,470 4 22 56

marked as duplicate by N 1.1, Daniel A. White, Nick Dandoulakis,


Naveen, Neil Butterworth Apr 16 '10 at 10:40
This question has been asked before and already has an answer. If those answers do not fully address your
question, please ask a new question.

4 Answers
A virtual function makes its class a polymorphic base class. Derived classes can override virtual functions.
Virtual functions called through base class pointers/references will be resolved at run-time. That is, the
dynamic type of the object is used instead of its static type:
Derived d;
Base& rb = d;
// if Base::f() is virtual and Derived overrides it, Derived::f() will be called
rb.f();
A pure virtual function is a virtual function whose declaration ends in =0 :
class Base {
// ...
virtual void f() = 0;
// ...
A pure virtual function makes the class it is defined for abstract. Abstract classes cannot be instantiated.
Derived classes need to override/implement all inherited pure virtual functions. If they do not, they too will
become abstract.
In C++, a class can define a pure virtual function that has an implementation. (What that's good for is
debatable.)
edited Sep 28 '12 at 7:26

answered Apr 16 '10 at 10:37


sbi
76.8k 13 105 228

For a virtual function you need to provide implementation in the base class. However derived class can
override this implementation with its own implementation. Normally , for pure virtual functions implementation

02/04/2013 10:06

c++ - Difference between a virtual function and a pure virtual function ...

2 of 2

http://stackoverflow.com/questions/2652198/difference-between-a-virt...

is not provided. You can make a function pure virtual with =0 at the end of function declaration. Also, a
class containing a pure virtual function is abstract i.e. you can not create a object of this class.
answered Apr 16 '10 at 10:38
Naveen
33.1k 12 71 147

You can actually provide implementations of pure virtual functions in C++. The only difference is all pure
virtual functions must be implemented by derived classes before the class can be instantiated.
answered Apr 16 '10 at 10:39
AshleysBrain
8,910 2 28 62

A pure virtual function is usually not (but can be) implemented in a base class and must be implemented in a
leaf subclass.
You denote that fact by appending the "= 0" to the declaration, like this:
class AbstractBase
{
virtual void PureVirtualFunction() = 0;
}
Then you cannot declare and instantiate a subclass without it implementing the pure virtual function:
class Derived : public AbstractBase
{
virtual void PureVirtualFunction() { }
}
edited Apr 18 '10 at 20:35

answered Apr 16 '10 at 10:36


Johann Gerell
8,289 16 47

1 In C++, a pure virtual function can be implemented. sbi Apr 16 '10 at 10:37
yes, and for the pure virtual destructor, it must be implemented. daramarak Apr 16 '10 at 10:39
@daramarak: pure virtual constructor ? in C++? Naveen Apr 16 '10 at 10:40
@Naveen: stackoverflow.com/questions/2609299/2609404#2609404 sbi Apr 16 '10 at 20:52
@sbi: I read at is constructor instead of destructor. My mistake.. Naveen Apr 17 '10 at 9:51
show 1 more comment

Not the answer you're looking for? Browse other questions tagged c++ function
virtual

pure-virtual or ask your own question.

02/04/2013 10:06

Das könnte Ihnen auch gefallen