pure^concrete virtual functions #bbg

3 types of virtual methods
* concrete virtual — virtual methods without “=0”. Regular virtual methods.
* concrete pure virtual – with “=0”. pure but with a body. See http://www.gotw.ca/gotw/031.htm. Least common.
* pure-pure virtual  — with “=0”. pure and without a body.  “Pure virtual” usually means this.

You can think of the “=0” as a NULL address assigned to the func ptr. Remember compiler maps each function NAME to an address of the function’s body. At first, a pure/abstract function has no body YET.
[2] concrete pure virtual????

Diff: if a class has at least one pure virtual, then you can’t [2] instantiate it. Subclass must implement it to become non-abstract – This is the purpose for the “PURE”.

Basically, all methods are implemented as func-ptr FIELDS and absolutely non-nullable in any class instance. With a func-ptr field in NULL, this class is non-instantiable i.e. “abstract”.

similar: Both PURE and concrete-virtual methods can have a “body” i.e. a method definition.
** Diff: But PURE declaration ends in “=0” so body must be somewhere else. Concrete virtual can have a body attached to declaration.

similar: the body is callable by subclasses
** Diff: but must be explicitly called in the PURE case. See http://www.gotw.ca/gotw/031.htm and also [[eff c++ 167]]

Q (I don’t remember the exact question at Bloomberg IV. A obscure question): pretend to be a compiler writer. How can your compiler intercept a call to an unimplemented PURE virtual method (i.e. a pure-pure virtual), with 0 runtime cost? P273 [[eff c#]] compares c# vs c++ when a pure virtual is invoked by ctor. It confirms c++ would crash.
%%A: Synthesize a body for each undefined PURE virtual. In the body, we can clean up and print diagnostic then call abort().
%%A: check the vtable. If the function pointer is null, then the host class can’t be instantiated in the first place.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s