I am pretty sure that a vtable entry for this function will be created.
This is due to the fact that once a function is declared virtual, it is
always virtual in all derived classes.  I don't think there is an exception
for pure virtual function declarations, but I am not completely positive.  I
compiled a quick little test using visual studio and derived a second class
from DerivedClass and add() is definitely treated as a virtual function.
This may differ from GCC.

A vtable in general won't be that large unless you have many virtual
functions.  The table only stores the addresses for each virtual function
and they are probably 4 bytes each.  CodeWarrior seems to have another 8
bytes per vtable that store other information as well.  So, if your base
class has 10 virtual functions and you derive 4 classes from your base class
you are consuming right around 160 + 32 = 192 bytes.  That is not that much
overhead.

I have a project that uses classes and a fair amount of virtual function and
it runs fine on an m500 with 8mb of memory.

~Brad

"L.M. Fridael" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
>
> Hi,
>
>
> Are there any hidden costs to using virtual functions in C++ classes using
> the PRC tools? (I know about the GCC compiler and linker switches to
disable
> exceptions and RTTI.)
>
> I know that polymorphism requires an in-memory table to ensure that the
> correct functions get called at run-time (late binding) and that this eats
> into the available heap size.  However, I want to use pure virtual
functions
> in my classes solely to specify a contract that subclasses must abide to.
I
> will not use base class pointers, only pointers to non-abstract classes.
>
> Example:
> -----------------------------------------------
> class BaseClass {
>     protected:
>         int a;
>     public:
>         virtual void add(int a) = 0;  // Pure virtual function
> };
>
> class DerivedClass: public BaseClass {
>     public:
>         DerivedClass(int a) {
>             this->a = a;
>         }
>         void add(int a); // Implementation omitted
> };
>
> BaseClass* a = new DerivedClass(1);
> a->add(1); // Polymorphism, late binding
> DerivedClass* b = new DerivedClass(1);
> b->add(1); // No polymorphism(?), early binding(?)
> -----------------------------------------------
>
> I have a gut feeling that the GCC compiler is smart enough to resolve the
> function call for "b" at compile-time, hence no need for a function table
at
> run-time, but I'm not sure. (It's been years since I last used C/C++ for a
> large project.)
>
> Also, how much memory does polymorphism actually require? Should you
really
> avoid it at all costs on Palm OS? My target is OS 3.5+ running on 4Mb+
> devices.
>
>
> Thanks
> -Laurens
>
>
>
>



-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to