Pure virtual destructor

S

santosh

Hello,
I was going through the Marshal Cline's C++ FAQ-Lite.

I have a doubt regarding section 33.10.



Here he is declaring a pure virtual destructor in the base class.

And again defining it inline. Like this.

inline Funct::~Funct() { } // defined even though it's pure virtual; it's
faster this way; trust me



My doubt is:

1.. Why we should make destructors pure virtual. What is the advantage?
2.. If it is pure virtual, there should not any definition of it in the
base class. It should be only defined in derived class, so that we can
create the instance of derived class.
3.. But in this case it is not redefined in derived class. What is the
reason?
4.. Again how it is fasted to make it define inline in Base class? Because
inline request will not be obeyed by compiler, if it is virtual.


I am attaching the 33.10 section of FAQ for your reference.





[33.10] What the heck is a functionoid, and why would I use one?

Functionoids are functions on steroids. Functionoids are strictly more
powerful than functions, and that extra power solves some (not all) of the
challenges typically faced when you use function-pointers.

Let's work an example showing a traditional use of function-pointers, then
we'll translate that example into functionoids. The traditional
function-pointer idea is to have a bunch of compatible functions:

int funct1(...params...) { ...code... }
int funct2(...params...) { ...code... }
int funct3(...params...) { ...code... }

Then you access those by function-pointers:

typedef int(*FunctPtr)(...params...);

void myCode(FunctPtr f)
{
...
f(...args-go-here...);
...
}

Sometimes people create an array of these function-pointers:

FunctPtr array[10];
array[0] = funct1;
array[1] = funct1;
array[2] = funct3;
array[3] = funct2;
...

In which case they call the function by accessing the array:

array(...args-go-here...);

With functionoids, you first create a base class with a pure-virtual method:

class Funct {
public:
virtual int doit(int x) = 0;
virtual ~Funct() = 0;
};

inline Funct::~Funct() { } // defined even though it's pure virtual; it's
faster this way; trust me

Then instead of three functions, you create three derived classes:

class Funct1 : public Funct {
public:
virtual int doit(int x) { ...code from funct1... }
};

class Funct2 : public Funct {
public:
virtual int doit(int x) { ...code from funct2... }
};

class Funct3 : public Funct {
public:
virtual int doit(int x) { ...code from funct3... }
};

Then instead of passing a function-pointer, you pass a Funct*. I'll create a
typedef called FunctPtr merely to make the rest of the code similar to the
old-fashioned approach:

typedef Funct* FunctPtr;

void myCode(FunctPtr f)
{
...
f->doit(...args-go-here...);
...
}

You can create an array of them in almost the same way:

FunctPtr array[10];
array[0] = new Funct1(...ctor-args...);
array[1] = new Funct1(...ctor-args...);
array[2] = new Funct3(...ctor-args...);
array[3] = new Funct2(...ctor-args...);
...

This gives us the first hint about where functionoids are strictly more
powerful than function-pointers: the fact that the functionoid approach has
arguments you can pass to the ctors (shown above as ...ctor-args...) whereas
the function-pointers version does not. Think of a functionoid object as a
freeze-dried function-call (emphasis on the word call). Unlike a pointer to
a function, a functionoid is (conceptually) a pointer to a partially called
function. Imagine for the moment a technology that lets you pass
some-but-not-all arguments to a function, then lets you freeze-dry that
(partially completed) call. Pretend that technology gives you back some sort
of magic pointer to that freeze-dried partially-completed function-call.
Then later you pass the remaining args using that pointer, and the system
magically takes your original args (that were freeze-dried), combines them
with any local variables that the function calculated prior to being
freeze-dried, combines all that with the newly passed args, and continues
the function's execution where it left off when it was freeze-dried. That
might sound like science fiction, but it's conceptually what functionoids
let you do. Plus they let you repeatedly "complete" that freeze-dried
function-call with various different "remaining parameters," as often as you
like. Plus they allow (not require) you to change the freeze-dried state
when it gets called, meaning functionoids can remember information from one
call to the next.

Okay, let's get our feet back on the ground and we'll work a couple of
examples to explain what all that mumbo jumbo really means.

Suppose the original functions (in the old-fashioned function-pointer style)
took slightly different parameters.

int funct1(int x, float y)
{ ...code... }

int funct2(int x, const std::string& y, int z)
{ ...code... }

int funct3(int x, const std::vector<double>& y)
{ ...code... }

When the parameters are different, the old-fashioned function-pointers
approach is difficult to use, since the caller doesn't know which parameters
to pass (the caller merely has a pointer to the function, not the function's
name or, when the parameters are different, the number and types of its
parameters) (do not write me an email about this; yes you can do it, but you
have to stand on your head and do messy things; but do not write me about
it - use functionoids instead).

With functionoids, the situation is, at least sometimes, much better. Since
a functionoid can be thought of as a freeze-dried function call, just take
the un-common args, such as the ones I've called y and/or z, and make them
args to the corresponding ctors. You may also pass the common args (in this
case the int called x) to the ctor, but you don't have to - you have the
option of passing it/them to the pure virtual doit() method instead. I'll
assume you want to pass x and into doit() and y and/or z into the ctors:

class Funct {
public:
virtual int doit(int x) = 0;
};

Then instead of three functions, you create three derived classes:

class Funct1 : public Funct {
public:
Funct1(float y) : y_(y) { }
virtual int doit(int x) { ...code from funct1... }
private:
float y_;
};

class Funct2 : public Funct {
public:
Funct2(const std::string& y, int z) : y_(y), z_(z) { }
virtual int doit(int x) { ...code from funct2... }
private:
std::string y_;
int z_;
};

class Funct3 : public Funct {
public:
Funct3(const std::vector<double>& y) : y_(y) { }
virtual int doit(int x) { ...code from funct3... }
private:
std::vector<double> y_;
};

Now you see that the ctor's parameters get freeze-dried into the functionoid
when you create the array of functionoids:

FunctPtr array[10];

array[0] = new Funct1(3.14f);

array[1] = new Funct1(2.18f);

std::vector<double> bottlesOfBeerOnTheWall;
bottlesOfBeerOnTheWall.push_back(100);
bottlesOfBeerOnTheWall.push_back(99);
...
bottlesOfBeerOnTheWall.push_back(1);
array[2] = new Funct3(bottlesOfBeerOnTheWall);

array[3] = new Funct2("my string", 42);

...

So when the user invokes the doit() on one of these functionoids, he
supplies the "remaining" args, and the call conceptually combines the
original args passed to the ctor with those passed into the doit() method:

array->doit(12);

As I've already hinted, one of the benefits of functionoids is that you can
have several instances of, say, Funct1 in your array, and those instances
can have different parameters freeze-dried into them. For example, array[0]
and array[1] are both of type Funct1, but the behavior of array[0]->doit(12)
will be different from the behavior of array[1]->doit(12) since the behavior
will depend on both the 12 that was passed to doit() and the args passed to
the ctors.

Another benefit of functionoids is apparent if we change the example from an
array of functionoids to a local functionoid. To set the stage, let's go
back to the old-fashioned function-pointer approach, and imagine that you're
trying to pass a comparison-function to a sort() or binarySearch() routine.
The sort() or binarySearch() routine is called childRoutine() and the
comparison function-pointer type is called FunctPtr:

void childRoutine(FunctPtr f)
{
...
f(...args...);
...
}

Then different callers would pass different function-pointers depending on
what they thought was best:

void myCaller()
{
...
childRoutine(funct1);
...
}

void yourCaller()
{
...
childRoutine(funct3);
...
}

We can easily translate this example into one using functionoids:

void childRoutine(Funct& f)
{
...
f.doit(...args...);
...
}

void myCaller()
{
...
Funct1 funct(...ctor-args...);
childRoutine(funct);
...
}

void yourCaller()
{
...
Funct3 funct(...ctor-args...);
childRoutine(funct);
...
}

Given this example as a backdrop, we can see two benefits of functionoids
over function-pointers. The "ctor args" benefit described above, plus the
fact that functionoids can maintain state between calls in a thread-safe
manner. With plain function-pointers, people normally maintain state between
calls via static data. However static data is not intrinsically
thread-safe - static data is shared between all threads. The functionoid
approach provides you with something that is intrinsically thread-safe since
the code ends up with thread-local data. The implementation is trivial:
change the old-fashioned static datum to an instance data member inside the
functionoid's this object, and poof, the data is not only thread-local, but
it is even safe with recursive calls: each call to yourCaller() will have
its own distinct Funct3 object with its own distinct instance data.

Note that we've gained something without losing anything. If you want
thread-global data, functionoids can give you that too: just change it from
an instance data member inside the functionoid's this object to a static
data member within the functionoid's class, or even to a local-scope static
data. You'd be no better off than with function-pointers, but you wouldn't
be worse off either.

The functionoid approach gives you a third option which is not available
with the old-fashioned approach: the functionoid lets callers decide whether
they want thread-local or thread-global data. They'd be responsible to use
locks in cases where they wanted thread-global data, but at least they'd
have the choice. It's easy:

void callerWithThreadLocalData()
{
...
Funct1 funct(...ctor-args...);
childRoutine(funct);
...
}

void callerWithThreadGlobalData()
{
...
static Funct1 funct(...ctor-args...); ? the static is the only
difference
childRoutine(funct);
...
}

Functionoids don't solve every problem encountered when making flexible
software, but they are strictly more powerful than function-pointers and
they are worth at least evaluating. In fact you can easily prove that
functionoids don't lose any power over function-pointers, since you can
imagine that the old-fashioned approach of function-pointers is equivalent
to having a global(!) functionoid object. Since you can always make a global
functionoid object, you haven't lost any ground. QED.

[ Top | Bottom | Previous section | Next section | Search the FAQ ]
 
A

Alf P. Steinbach

* santosh:
I was going through the Marshal Cline's C++ FAQ-Lite.

I have a doubt regarding section 33.10.

That's the one titled "What the heck is a functionoid, and why would I use
one?".

It would have been nice if you had included that title so we could know
what the, uhm, well, you were talking about! :)

I have a doubt regarding the term "functionoid"; these small objects are
usually called "functors" in C++, and other things in other languages, and
I haven't encountered the term "functionoid" anywhere else but in the FAQ.

Here he is declaring a pure virtual destructor in the base class.

And again defining it inline. Like this.

inline Funct::~Funct() { } // defined even though it's pure virtual; it's
faster this way; trust me

There seems to be no reason to declare the destructor in that class as
pure virtual; I don't know why it is (editing blunder?).

My doubt is:

1.. Why we should make destructors pure virtual. What is the advantage?

If you have no other function at hand the destructor is a convenient one
to make pure virtual.

That makes the class effectively abstract at the language level.

It's often done for so-called "marker interfaces".

2.. If it is pure virtual, there should not any definition of it in the
base class. It should be only defined in derived class, so that we can
create the instance of derived class.

No, that is incorrect.

Since the destructor is called there _must_ be a definition of it.

3.. But in this case it is not redefined in derived class. What is the
reason?

The compiler automatically generates a destructor if none is declared.

4.. Again how it is fasted to make it define inline in Base class? Because
inline request will not be obeyed by compiler, if it is virtual.

I think that comment about "faster" (as well as the destructor being pure
virtual) must be an editing blunder, perhaps something from old text about
something else.

I am attaching the 33.10 section of FAQ for your reference.

DON'T DO THAT!

Some people still read news over slow analog lines.

Please do be considerate; give an URL if necessary.
 
A

Alf P. Steinbach

* santosh:
Because
inline request will not be obeyed by compiler, if it is virtual.

Sorry, that is incorrect.

Not that 'inline' is a request that the compiler is bound to obey
anyway.

But the compiler is free to inline calls of a virtual function, and
if it's a good compiler, it will, where appropriate.
 
S

Srini

My doubt is:
1.. Why we should make destructors pure virtual. What is the advantage?

Pure virtual functions are used to make classes abstract. One can make
a destructor pure virtual when a class may not have any other candidate
member functions that can be made pure virtual. In such a case, instead
of having a dummy pure virtual function, you can make the destructor
pure virtual.
2.. If it is pure virtual, there should not any definition of it in the
base class. It should be only defined in derived class, so that we can
create the instance of derived class.

Pure virtual destructors _have_ to have a definition. This is because,
in an inheritance heirarchy while destroying a derived class object,
all the destructors in the class heirarchy are called. If you could
leave out the definition of the pure virtual destructor, then there
would be no destructor function body to call.
3.. But in this case it is not redefined in derived class. What is the
reason?

Pure virtual destructors need not be defined in the derived class. The
reason is this. Normal pure virtual functions in a base class would
cause the derived class to be abstract unless it is defined in the
derived class. But destructors are automatically supplied by the
compiler if you don't give one yourself.
4.. Again how it is fasted to make it define inline in Base class? Because
inline request will not be obeyed by compiler, if it is virtual.

I'm not sure about how it would be faster.

Regards,
Srini

Hello,
I was going through the Marshal Cline's C++ FAQ-Lite.

I have a doubt regarding section 33.10.



Here he is declaring a pure virtual destructor in the base class.

And again defining it inline. Like this.

inline Funct::~Funct() { } // defined even though it's pure virtual; it's
faster this way; trust me



My doubt is:

1.. Why we should make destructors pure virtual. What is the advantage?
2.. If it is pure virtual, there should not any definition of it in the
base class. It should be only defined in derived class, so that we can
create the instance of derived class.
3.. But in this case it is not redefined in derived class. What is the
reason?
4.. Again how it is fasted to make it define inline in Base class? Because
inline request will not be obeyed by compiler, if it is virtual.


I am attaching the 33.10 section of FAQ for your reference.





[33.10] What the heck is a functionoid, and why would I use one?

Functionoids are functions on steroids. Functionoids are strictly more
powerful than functions, and that extra power solves some (not all) of the
challenges typically faced when you use function-pointers.

Let's work an example showing a traditional use of function-pointers, then
we'll translate that example into functionoids. The traditional
function-pointer idea is to have a bunch of compatible functions:

int funct1(...params...) { ...code... }
int funct2(...params...) { ...code... }
int funct3(...params...) { ...code... }

Then you access those by function-pointers:

typedef int(*FunctPtr)(...params...);

void myCode(FunctPtr f)
{
...
f(...args-go-here...);
...
}

Sometimes people create an array of these function-pointers:

FunctPtr array[10];
array[0] = funct1;
array[1] = funct1;
array[2] = funct3;
array[3] = funct2;
...

In which case they call the function by accessing the array:

array(...args-go-here...);

With functionoids, you first create a base class with a pure-virtual method:

class Funct {
public:
virtual int doit(int x) = 0;
virtual ~Funct() = 0;
};

inline Funct::~Funct() { } // defined even though it's pure virtual; it's
faster this way; trust me

Then instead of three functions, you create three derived classes:

class Funct1 : public Funct {
public:
virtual int doit(int x) { ...code from funct1... }
};

class Funct2 : public Funct {
public:
virtual int doit(int x) { ...code from funct2... }
};

class Funct3 : public Funct {
public:
virtual int doit(int x) { ...code from funct3... }
};

Then instead of passing a function-pointer, you pass a Funct*. I'll create a
typedef called FunctPtr merely to make the rest of the code similar to the
old-fashioned approach:

typedef Funct* FunctPtr;

void myCode(FunctPtr f)
{
...
f->doit(...args-go-here...);
...
}

You can create an array of them in almost the same way:

FunctPtr array[10];
array[0] = new Funct1(...ctor-args...);
array[1] = new Funct1(...ctor-args...);
array[2] = new Funct3(...ctor-args...);
array[3] = new Funct2(...ctor-args...);
...

This gives us the first hint about where functionoids are strictly more
powerful than function-pointers: the fact that the functionoid approach has
arguments you can pass to the ctors (shown above as ...ctor-args...) whereas
the function-pointers version does not. Think of a functionoid object as a
freeze-dried function-call (emphasis on the word call). Unlike a pointer to
a function, a functionoid is (conceptually) a pointer to a partially called
function. Imagine for the moment a technology that lets you pass
some-but-not-all arguments to a function, then lets you freeze-dry that
(partially completed) call. Pretend that technology gives you back some sort
of magic pointer to that freeze-dried partially-completed function-call.
Then later you pass the remaining args using that pointer, and the system
magically takes your original args (that were freeze-dried), combines them
with any local variables that the function calculated prior to being
freeze-dried, combines all that with the newly passed args, and continues
the function's execution where it left off when it was freeze-dried. That
might sound like science fiction, but it's conceptually what functionoids
let you do. Plus they let you repeatedly "complete" that freeze-dried
function-call with various different "remaining parameters," as often as you
like. Plus they allow (not require) you to change the freeze-dried state
when it gets called, meaning functionoids can remember information from one
call to the next.

Okay, let's get our feet back on the ground and we'll work a couple of
examples to explain what all that mumbo jumbo really means.

Suppose the original functions (in the old-fashioned function-pointer style)
took slightly different parameters.

int funct1(int x, float y)
{ ...code... }

int funct2(int x, const std::string& y, int z)
{ ...code... }

int funct3(int x, const std::vector<double>& y)
{ ...code... }

When the parameters are different, the old-fashioned function-pointers
approach is difficult to use, since the caller doesn't know which parameters
to pass (the caller merely has a pointer to the function, not the function's
name or, when the parameters are different, the number and types of its
parameters) (do not write me an email about this; yes you can do it, but you
have to stand on your head and do messy things; but do not write me about
it - use functionoids instead).

With functionoids, the situation is, at least sometimes, much better. Since
a functionoid can be thought of as a freeze-dried function call, just take
the un-common args, such as the ones I've called y and/or z, and make them
args to the corresponding ctors. You may also pass the common args (in this
case the int called x) to the ctor, but you don't have to - you have the
option of passing it/them to the pure virtual doit() method instead. I'll
assume you want to pass x and into doit() and y and/or z into the ctors:

class Funct {
public:
virtual int doit(int x) = 0;
};

Then instead of three functions, you create three derived classes:

class Funct1 : public Funct {
public:
Funct1(float y) : y_(y) { }
virtual int doit(int x) { ...code from funct1... }
private:
float y_;
};

class Funct2 : public Funct {
public:
Funct2(const std::string& y, int z) : y_(y), z_(z) { }
virtual int doit(int x) { ...code from funct2... }
private:
std::string y_;
int z_;
};

class Funct3 : public Funct {
public:
Funct3(const std::vector<double>& y) : y_(y) { }
virtual int doit(int x) { ...code from funct3... }
private:
std::vector<double> y_;
};

Now you see that the ctor's parameters get freeze-dried into the functionoid
when you create the array of functionoids:

FunctPtr array[10];

array[0] = new Funct1(3.14f);

array[1] = new Funct1(2.18f);

std::vector<double> bottlesOfBeerOnTheWall;
bottlesOfBeerOnTheWall.push_back(100);
bottlesOfBeerOnTheWall.push_back(99);
...
bottlesOfBeerOnTheWall.push_back(1);
array[2] = new Funct3(bottlesOfBeerOnTheWall);

array[3] = new Funct2("my string", 42);

...

So when the user invokes the doit() on one of these functionoids, he
supplies the "remaining" args, and the call conceptually combines the
original args passed to the ctor with those passed into the doit() method:

array->doit(12);

As I've already hinted, one of the benefits of functionoids is that you can
have several instances of, say, Funct1 in your array, and those instances
can have different parameters freeze-dried into them. For example, array[0]
and array[1] are both of type Funct1, but the behavior of array[0]->doit(12)
will be different from the behavior of array[1]->doit(12) since the behavior
will depend on both the 12 that was passed to doit() and the args passed to
the ctors.

Another benefit of functionoids is apparent if we change the example from an
array of functionoids to a local functionoid. To set the stage, let's go
back to the old-fashioned function-pointer approach, and imagine that you're
trying to pass a comparison-function to a sort() or binarySearch() routine.
The sort() or binarySearch() routine is called childRoutine() and the
comparison function-pointer type is called FunctPtr:

void childRoutine(FunctPtr f)
{
...
f(...args...);
...
}

Then different callers would pass different function-pointers depending on
what they thought was best:

void myCaller()
{
...
childRoutine(funct1);
...
}

void yourCaller()
{
...
childRoutine(funct3);
...
}

We can easily translate this example into one using functionoids:

void childRoutine(Funct& f)
{
...
f.doit(...args...);
...
}

void myCaller()
{
...
Funct1 funct(...ctor-args...);
childRoutine(funct);
...
}

void yourCaller()
{
...
Funct3 funct(...ctor-args...);
childRoutine(funct);
...
}

Given this example as a backdrop, we can see two benefits of functionoids
over function-pointers. The "ctor args" benefit described above, plus the
fact that functionoids can maintain state between calls in a thread-safe
manner. With plain function-pointers, people normally maintain state between
calls via static data. However static data is not intrinsically
thread-safe - static data is shared between all threads. The functionoid
approach provides you with something that is intrinsically thread-safe since
the code ends up with thread-local data. The implementation is trivial:
change the old-fashioned static datum to an instance data member inside the
functionoid's this object, and poof, the data is not only thread-local, but
it is even safe with recursive calls: each call to yourCaller() will have
its own distinct Funct3 object with its own distinct instance data.

Note that we've gained something without losing anything. If you want
thread-global data, functionoids can give you that too: just change it from
an instance data member inside the functionoid's this object to a static
data member within the functionoid's class, or even to a local-scope static
data. You'd be no better off than with function-pointers, but you wouldn't
be worse off either.

The functionoid approach gives you a third option which is not available
with the old-fashioned approach: the functionoid lets callers decide whether
they want thread-local or thread-global data. They'd be responsible to use
locks in cases where they wanted thread-global data, but at least they'd
have the choice. It's easy:

void callerWithThreadLocalData()
{
...
Funct1 funct(...ctor-args...);
childRoutine(funct);
...
}

void callerWithThreadGlobalData()
{
...
static Funct1 funct(...ctor-args...); ? the static is the only
difference
childRoutine(funct);
...
}

Functionoids don't solve every problem encountered when making flexible
software, but they are strictly more powerful than function-pointers and
they are worth at least evaluating. In fact you can easily prove that
functionoids don't lose any power over function-pointers, since you can
imagine that the old-fashioned approach of function-pointers is equivalent
to having a global(!) functionoid object. Since you can always make a global
functionoid object, you haven't lost any ground. QED.

[ Top | Bottom | Previous section | Next section | Search the FAQ ]
 
S

Sam

Srini said:
Pure virtual functions are used to make classes abstract. One can make
a destructor pure virtual when a class may not have any other candidate
member functions that can be made pure virtual. In such a case, instead
of having a dummy pure virtual function, you can make the destructor
pure virtual.



Pure virtual destructors _have_ to have a definition. This is because,
in an inheritance heirarchy while destroying a derived class object,
all the destructors in the class heirarchy are called. If you could
leave out the definition of the pure virtual destructor, then there
would be no destructor function body to call.
Correct me if I wrong. The meaning of pure virtual is has no definition
in the base class. Now you said destructor can be pure virtual, but need
to have definition. I m a bit confuse what you said here. If a
function has to have definition, it is not pure virtual.

eg.
virtual ~destructor() = 0;

Do you meant the above example is illegal?

Sam.
 
S

Sam

Rolf said:
Sam wrote:




It must be overridden by derived classes.
I thought pure virtual means has no definition and derived class must
define its implementation. I remembered you mentioned that the base
class has a vritual keyword on the ~desutrctor, but with definition or
implementation for the pure virtual destructor.

Sam
 
V

Victor Bazarov

Sam said:
I thought pure virtual means has no definition and derived class must
define its implementation.

Both are wrong.
I remembered you mentioned that the base
class has a vritual keyword on the ~desutrctor, but with definition or
implementation for the pure virtual destructor.

There is no connection between "pure" and "has no definition" except that
you _may_ omit a definition of a pure virtual function *unless* it is
the destructor. And in most cases, that's what people do, they declare
virtual functions as pure because there is nothing that can represent the
behaviour of the object of that class when that function is called -- the
behaviour will be provided by the derived classes, and only those that do
actually have it.

Now, as to "must define": only the _concrete_ class has to have all its
virtual functions defined. It stems from the definition of the concrete
class. IOW, if I have a class that derives from an abstract class *and*
does *not* define some of the pure virtual functions, my derived class
is simply abstract, just like the one it derives from.

V
 
D

Donovan Rebbechi

There is no connection between "pure" and "has no definition" except that
you _may_ omit a definition of a pure virtual function *unless* it is
the destructor. And in most cases, that's what people do, they declare
virtual functions as pure because there is nothing that can represent the
behaviour of the object of that class when that function is called -- the
behaviour will be provided by the derived classes, and only those that do
actually have it.

Indeed. And one could provide a base class version, for the derived class to
call (much like the way the destructor works)

#include <iostream>

struct Foo { virtual void bar() = 0 ; }; // pure virtual
// yet we provide a definition
void Foo::bar() { std::cout << "Foo" << std::endl; }
struct Bar : Foo {
virtual void bar () {
Foo::bar(); // and we call the pure virtual fn
std::cout << "Bar" << std::endl;
}
};
int main() { Foo* b = new Bar; b->bar(); }

Cheers,
 
P

Peter Julian

Sam said:
I thought pure virtual means has no definition and derived class must
define its implementation. I remembered you mentioned that the base
class has a vritual keyword on the ~desutrctor, but with definition or
implementation for the pure virtual destructor.

Sam

Thats Java's definition of an abstract class you are referring to (called an
interface in Java). In C++ you can provide definitions in both the
pure-virtual and the derived class. This means that you can place the
implementation and members that are common to the derived classes in a
central location without having to duplicate code repeatedly.

Consider the following example where a shape's area is calculated in the
derived class's initialization list and then stored in the pure-virtual
component. Note the call to the pure-virtual Shape::getArea() in the derived
Rectangle's getArea() member function:

#include <iostream>
#include <vector>

// pure virtual class Shape
//
class Shape
{
double area;
public:
Shape(double a) : area(a) { }
virtual ~Shape() { }
virtual double getArea() const = 0 // pure-virtual
{
return area;
}
};

// derived class Rectangle
//
class Rectangle : public Shape
{
double height;
double width;
public:
Rectangle(double h, double w) : height(h), width(w), Shape(h * w) { }
~Rectangle() { }
double getArea() const // polymorphic overide
{
std::cout << "rectangle:\t";
return Shape::getArea(); // call abstract member function
}
};

int main()
{
std::vector<Shape*> vshapes;

// vshapes.push_back(new Shape(1.0)); // not allowed

vshapes.push_back(new Rectangle(1.0, 1.0));
vshapes.push_back(new Rectangle(2.0, 2.0));
vshapes.push_back(new Rectangle(3.0, 3.0));

for (size_t i = 0; i < vshapes.size(); ++i)
{
std::cout << "area of shape[" << i << "], which is a ";
std::cout << vshapes->getArea();
std::cout << std::endl;
}

for (size_t j = 0; j < vshapes.size(); ++j)
{
delete vshapes[j];
}

return 0;
}

This simplifies creation of other derived shapes as well. The only
requirement is to calculate the area in the derived class's init list. Don't
all shapes have an area? What about colour? Think about all the attributes,
like coordinate location, that you might have to add to a project with a
dozen derived shapes.
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top