Re: some abstract base class dont need vtbl?
petke wrote:
I don't understand. How can you enforce a common interface on a family
of classes by deriving them from an empty base class? If you want
classes to be related via runtime polymorphism, you need virtual
functions, otherwise you don't.
Runtime polymorphism isn't what im looking for.
See below.
I think this other type
of inheritance is the most useful type of all. Just to make clear what
I mean. In java the keyword interface is heavily used. In c++ we do it
by abstract base classes. By publicly inheriting these kinds of classes
we only inherit the interface, and no implementation. (To reuse any
implementation it is considered better to use aggregation or protected
inheritance anyway). We use objects thru their abstract interface and
we only commit to the smallest interface we can. As in;
//Pseudo code..
Class Phil : public WorkerABI, public BeerDrinkerABI {...}
Class Joe : public WorkerABI, public BeerDrinkerABI {...}
...
If(!weekend) {
WorkerAbstarctBase aPtr1 * = new Phil();
aPtr1.work();
aPtr1 = new Joe();
aPtr1.work();
}else {
BeerDrinkerAbstarctBase aPtr1 * = GetRandomBeerDrinker();
aPtr1.drink();
}
Well that might be a bad example
I think it is. Phil/Joe sound more like objects than classes. But OK.
but I need it that to answer your
question: "How can you enforce a common interface on a family of
classes by deriving them from an empty base class"
Well any class (Joe or Phil) that want to belong to the family of
workers or beer drinkers has to publicly derive from the abstract base
WorkerABI or BeerDrinkerABI. If it didn't the above code would not
compile. That's because I use the object by an abstract base class
pointer. That is how I can enforce the interface of object of the same
family.
I hope that made some sense.
I don't understand. This is runtime polymorphism. Any time you have
an object of class Phil or class Joe you can use it where an object of
type WorkerABI is required.
To make it work, you must declare all the functions of WorkerABI that
you want overridden by Phil and Joe as virtual.
To get back to your original question, the compiler has no need to
create a vtable for a class if the class has at least one pure virtual
function (so objects of the class cannot be created), and if the
constructor and destructor are empty (or more strictly: cannot invoke
directly or indirectly any virtual functions).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]