LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   CPP: Question about memory consumption of many vitural functions in a class of mine. (https://www.linuxquestions.org/questions/programming-9/cpp-question-about-memory-consumption-of-many-vitural-functions-in-a-class-of-mine-550070/)

RHLinuxGUY 04-30-2007 03:07 AM

CPP: Question about memory consumption of many vitural functions in a class of mine.
 
I was thinking about it, and I want to know. If I have about 30-35 virtual functions with empty brackets instead of "return_type function () = 0;"'s, how much memory would it take? Better question, how much memory would one empty bracket virtual function would should take up? Is this a bad design choice? Should I scrap it and go with GTK+'s way of changing data? (Through external functions instead of class methods?) Or would I not see the impact?

If you are wondering what this is for, (but I guess this wouldn't matter if it is a bad design choice, but I don't know) it is for a simple Mario clone.

Thanks in advance.

graemef 04-30-2007 08:31 PM

I wouldn't say that having empty virtual functions is a bad design, although there may be cases where taht is true. Additioanly I'd say that the overhead of virtual functions is minimal.

Essentially, the compiler will add code to look up and find out which function is to be called, based upon the class of the object at run time. It is better to make the function pure if it should be implemented by all child classes.

The advantage of virtual functions is that it makes the code cleaner. You don't need to worry about the class of the object in the code, the compiler will do that for you.

tuxdev 04-30-2007 08:43 PM

Virtual methods generally stored on a per-class basis, not a per-object basis.
In my experience, I've only seen empty brackets for either something like the Adapter pattern, or for a virtual destructor.

GTK+ does things the C way. C++ that is coded like C is a common problem, and generally means you're either using the wrong language or the wrong design for the language.

Also, something to remember:
The Three Rules of Optimization
1. Don't optimize
2. Don't optimize
3. Don't optimize much

"Get it working, get it right, get it fast." -- Kent Beck (I think)

RHLinuxGUY 04-30-2007 11:01 PM

Thats good to hear.

Quote:

Originally Posted by tuxdev
Virtual methods generally stored on a per-class basis, not a per-object basis.

So I think I understand what you mean. So instead of having a copy of a class method for each object. There is one that works for all. Just like how GTK+ does it right?

e.g.

void chg_field_in_child ( child * obj ) { /* ... */ }

Where there is one function that changes all. In the class of virtual functions, there exists only one method that works for all classes that use it. That is how I understand what you wrote.


All times are GMT -5. The time now is 07:30 AM.