Le 01/11/10 12:46, MartinBroadhurst a écrit :
> On 1 Nov, 11:40, jacob navia<ja...@spamsink.net> wrote:
>>
>> Well, then it corresponds exactly to an abstract structure in C.
>> (struct foo
>>
>> Do an abstract class force a performance penalty in C++?
>>
>
> Yes, because there is an extra level of indirection. When a method is
> called on a pointer to an abstract base class, the vtable is retrieved
> from the address point of the object, and then the derived class
> method pointer is retrieved from the vtable.
That is exactly what I do:
For instance to call the generic function "newIterator":
Iterator *newIterator(GenericContainer *gen)
{
return gen->VTable->newIterator(gen);
}
I call the vtable of the object. Since the vtables of all
containers contain the function "newIterator" at the same position,
this works.
I do it with ONE indirection, and not two, as you can see.
OK, then if I am at the same level of C++ it is OK.
>
>> When I call an abstract class, does the generated code have more to do?
>>
>
> Yes, for the same reason. In addition, there is some overhead in
> constructing objects with virtual functions because the vtable needs
> to be set up.
Since I do only one indirection, this is not necessary. Derived objects
are just the same as before.
The only thing that the derived object needs to ensure is that all
functions that are part of the interface are at the same position in
their vtables.
After that, there are no constraints, in fact, the existence of the
abstract class is completely transparent to them.
What is relly nice when working in C is the absolute freedom to do as
you want. In this case, not having ANY object oriented framework is a
bonus in some ways since you can lay out your object as you want.
jacob