LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-30-2007, 03:07 AM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
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.
 
Old 04-30-2007, 08:31 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 04-30-2007, 08:43 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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)
 
Old 04-30-2007, 11:01 PM   #4
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889

Original Poster
Blog Entries: 1

Rep: Reputation: 30
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CPP: Is it possible to use the members of an inherited class? RHLinuxGUY Programming 5 04-30-2007 02:48 AM
PHP: Calling class functions from another class. unreal128 Programming 5 11-24-2006 03:04 AM
Memory Consumption of 10.2 naparhi Slackware 4 08-27-2006 03:10 AM
process memory consumption ilnli Linux - General 4 09-08-2005 07:13 PM
Slackware memory consumption muhkuhmasta Slackware 5 07-15-2004 05:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration