CPP: Question about memory consumption of many vitural functions in a class of mine.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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)
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.