On Thursday 05 October 2006 16:48, Albert Zeyer wrote:
> Hi
>
> I am working on a very huge object-system, that means, I will have
> thousands objects and more in the memory.
> I am now thinking on how to redrucing the needed memory for each object
> to a minimum.
>
> The base of all objects is abstract, that means, I could use an
> interface for it or an abstract class with virtual abstract functions.
> I will have much helper functions on each object (~ 30 at the moment),
> that don't need to be virtual, because they should always behave the
> same, they internally simply use the other virtual functions. I don't
> want them outside the abstract class or the interface, because I don't
> want to have a mixture between a procedural and an object-oriented code.
> But the count of virtual functions is also not very small (~ 10 - 20).
>
> If I have a TObject1 and a TObject2 which both implements the abstract
> class TBaseObject, then what exactly is saved internally by an instance
> of each class? Does they contain a full table for each virtual function
> (that means ~ 40 - 80 bytes only for this table) or does they simply
> have a reference to their implementor-class (this means 4 bytes)?

They simply have a reference to the TClass. Interfaces are much like this, 
although not exactly (but they scale as O(1) instead of O(n), with n objects)

> I will have perhaps not more than 5 different implementor-classes, so if
> a virtual function table is saved for every object, this table would
> look mostly the same on all objects.

It's only saved for every class.

> If I use an interface instead of the abstract class, is it the same
> situation or is the virtual function handling different?

It's _almost_ the same. You don't need to worry about the difference, it's 
only important when programming the compiler.

> But if I use an interface, where should I declare all my helper
> functions for the object? If I declare them all in the interface, the
> interface would be very huge.

You should make an extra interface, which can be accessed by QueryInterface(). 
Then, on implementations that support the helper functions, you return the 
helper interface. On other implementations, you return nil.

> And if I don't want to define the helper functions again and again for
> each implementor (that means to recode always the same), I have to have
> an abstract class again because else there is no way to declare them
> elsewhere.

There are several ways that interfaces can help you there - one of them would 
be the IMPLEMENTS-keyword.

> I will have situations, where I want to create a huge amount of these
> objects, therefore it is important to save memory for each instance.

You're not much worse off by using interfaces. A few thousand objects should 
take only a few KB more mem. I hope you're not THAT pressed for memory? :)

-- 
Regards,
Christian Iversen

_________________________________________________________________
     To unsubscribe: mail [EMAIL PROTECTED] with
                "unsubscribe" as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to