I've used virtual functions without noticeable problems, but I have never come close to using the entire heap where optimizing such things would become a priority. I imagine at most it takes a few bytes per virtual function per object, but I too would like to know the specifics. I'll bet that the compiler always creates the vtable because it can't know how you are going to access the object in the future. Any discussion on this?
I firmly believe that you shouldn't avoid virtual functions altogether. Use them where it really makes sense. There are other ways to optimize your heap space use.
-Ted
At 15:10 06/08/2003 +0200, you wrote:
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/
Ted Beisler SPRUCE Computer Systems, Inc. [EMAIL PROTECTED]
-- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
