pure virtual

G

Guest

I don't understand. What's the difference in practice between 2 next lines?
When I *must* use first line and when second?
 
S

Siana

I don't understand. What's the difference in practice between 2 next lines?
When I *must* use first line and when second?

The first line declares a function a in some class that has no
definition - it is there only to be overridden by derived classes for
the purposes of polymorphism. It also makes the class abstract (unable
to be instantiated). Derived classes must declare and implement this
function or else they too are abstract and un-instatiable. It is
useful in defining an interface.

The second line declares a function a in some class that must have an
implementation for that class. That implementation will be inherited
by derived classes but can still be overridden and perform
polymorphically.

S.
 
T

Thomas Wintschel

I don't understand. What's the difference in practice between 2 next lines?
When I *must* use first line and when second?

You may want to look up 'pure virtual'

The first one would be used when defining a pure virtual base class. This
is done in situations where code needs to call the method 'a' for objects of
types which derive from the class but where you need the derived class to
implement the actual method (derived class *must* implement the method). If
we define:

class C
{
 
K

Kevin Goodsell

Siana said:
The first line declares a function a in some class that has no
definition

That is not true in general. A pure virtual function certainly may have
a definition.
- it is there only to be overridden by derived classes for
the purposes of polymorphism. It also makes the class abstract (unable
to be instantiated). Derived classes must declare and implement this
function or else they too are abstract and un-instatiable. It is
useful in defining an interface.

-Kevin
 
R

Ron Natalie

Siana said:
The first line declares a function a in some class that has no
definition

Wrong answer. It is the designation of the function as pure virtual.
It does not preclude the function from having a definition. It makes
the class it is defined in abstract. An abstract class is one that can
not be instantiated. It serves only as a base class for others.
- it is there only to be overridden by derived classes for
the purposes of polymorphism.

Not quite, it means that it must be overridden in dervied classes
(as well as all other pure virtual functions) in order to be concrete
(that is, one that can have instances created of it).
The second line declares a function a in some class that must have an
implementation for that class. That implementation will be inherited
by derived classes but can still be overridden and perform
polymorphically.

No it just declares one that is not pure. It means that it doesn't make
the class abstract.

While it is the case that you can omit the implementation of a pure virtual
function, that is secondary to it's meaning.
 
J

jeffc

I don't understand. What's the difference in practice between 2 next lines?
When I *must* use first line and when second?

There is no time when you *must* use the first (pure virtual). It's a
design decision on your part. There are 2 reasons you might use it
a) you want to force any class that inherits from this class to define
function a
b) you want that class to be abstract (no one can make an instance of it -
you can only make instances of subclasses). This is a rather confusing
means of creating an abstract class, but that's the way it is.
 
J

jeffc

Ron Natalie said:
Now you're being pedantic :)

I knew somone was going to say that :) The point to my way of thinking is
that "pure virtual base class" is not an OO term, it's not a C++ term, it's
just not any term I've ever heard. I did not of course object to "method",
nor would I even have objected if he used the term "abstract method" for
pure virtual function. I'd be interested in knowing if that's what Thomas
really meant to write and where he heard it. Maybe I should have said "You
mean an abstract class, right?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top