TITLE: implementations for pure virtual functions


PROBLEM: Vadla (vadla@aol.com)

[...]

If I am not mistaken a pure virtual function can have a body ie

class Foo
{   
 public:
 	Foo(){};
 	virtual ~Foo(){};
 	virtual char *GetName()= 0 {return szName;}
 private:
	char *szName;
	
};

[...]

My question is; what exactly does this mean??  I know that pure virtual
functions define the behaviour of a class but what does adding the boy
mean??


RESPONSE: kanze@gabi-soft.fr (J. Kanze), 29 Sept 95

[...]

What does what mean?  Making a function pure virtual means that it
cannot be called by the virtual function mechanism.  That's all.  And it
makes it legal to not have a body, if you don't want to.  It also makes
the class abstract, so it cannot be instantiated.

Basically, when you define a virtual function in a base class you have
three choices:

1. Provide a default behavior.  In this case, you also must provide an
implementation.

2. Not provide a default behavior.  In this case, you declare the
function pure virtual, and do not provide an implementation.

3. Provide a default behavior, but require the derived class to declare
explicitly that it wants the default behavior.  In this case, you
declare the function pure virtual, but also provide an implementation.
Example:

	class B
	{
	public :
		virtual void f() = 0 ;	//  declared pure virtual
	} ;

	void
	B::f()	//	but with an implementation
	{
		//  ...
	}

	class D : public B
	{
	public :
		virtual void f() ;	//	must be declared
	} ;

	void
	D::f()	//	and has an implementation
	{
		B::f() ;	//	which is in fact the default
	}

In general, a pure virtual function can be called in several manners:

1. As above, using a scope resolution operator.

2. In the constructor or destructor of the class.

The second case is generally an error.
