On Wednesday 25 July 2001 16:55, Lars Gullik Bj�nnes wrote:
> Angus Leeming <[EMAIL PROTECTED]> writes:
>
> | We currently have a hierarchy of Inset classes so:
> |
> | class Inset {};
> | class InsetButton : public Inset {};
> | class InsetCommand: public InsetButton {};
> | class InsetCitation : public InsetCommand {};
> |
> | I'd like to split InsetButton into InsetEditableButton and
> | InsetNonEditableButton() and to use virtual base classes:
>
> Please teach me the difference between a "base class" and a "virtual
> base class" first. (I never understood this...)
Lars! (shocked voice!)
How about: one compiles and the other doesn't ;-)
Actually, whilst no doubt Andre will correct me here, I think I know:
If we have "normal" base classes, InsetCitation will contain two instances of
Inset
Inset Inset
| |
InsetEditableButton InsetCommand
\ /
InsetCitation
If we have virtual base classes the internal structure looks like this:
InsetEditableButton InsetCommand Inset
\ | /
InsetCitation
Paraphrasing C++ Annotations (from the recommended reading list!), page 335:
The fact that Inset is no longer "embedded" in InsetEditableButton or
InsetCommand has a consequence for the chain of construction. the constructor
of an InsetCitation will call directly the constructor for an Inset; this
constructor will not be called from the constructors of either
InsetEditableButton or InsetCommand.
Note that defining either InsetEditableButton or InsetCommand as having
virtual derivation is sufficient. The compiler works out the rest.
Angus
Errors when not "virtual"
cxx -std strict_ansi -nocleanup -o trial buttonSuggestion.C
cxx: Error: buttonSuggestion.C, line 45: object of abstract class type
"InsetCitation" is not allowed:
pure virtual function "Inset::draw" has no overrider
InsetCitation example;
----------------------^
cxx: Error: buttonSuggestion.C, line 46: "InsetCitation::draw" is ambiguous
example.draw();
----------------^
cxx: Info: 2 errors detected in the compilation of "buttonSuggestion.C".