[LC++]Pure Virtual Functions and Derived Classes

Jack Lloyd lloyd at acm.jhu.edu
Wed May 1 01:35:06 UTC 2002


On Mon, 29 Apr 2002, Marshall Pharoah wrote:

> Yes, derived classes must contain a defiition for all virtual functions.

IF you wish to instantiate the class in question. This is pefectly legal
(and often very useful):

class A
 {
 virtual void foo() = 0;
 virtual void bar() = 0;
 };

class B : public A
 {
 void foo() { /* do something */ }
 };

class C : public B
 {
 void bar() { /* do something else */ }
 };

You can't create an A or B, but you can create C. But you can certainly
pass around pointers or references to A or B, and all are valid
declarations.

> Also, you may not create an instance of a firtual base class.

Be careful with a statement like that. "virtual base class" means something
very different from "a base class with (pure) virtual functions"
(specifically, deriving using virtual inheritence).

-Jack





More information about the tuxCPProgramming mailing list