Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

NuffSaid

C++ stuff

This topic is 6410 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just wondering, if performance wasn''t a problem, would it be a good idea to make all functions virtual? Even if there was a slight performance hit, is it even noticeable? Another thing, when you declare a member variable as private, this variable isn''t accesible by the child classes of the afore mentioned class, right? If what I just said is correct, would it be better to make member variables protected (since the child class will then be able to access/modify the variable)? Or am I just totally screwed up with my OO concepts and should just stick to plain old C? Philip

Share this post


Link to post
Share on other sites
Advertisement
In the ideal world, if virtual functions had no performance overhead, then yes, you could go make almost every function virtual. Of course, static functions cannot be virtual and static functions are useful things to have around. There are probably some other cases where having the functions not be virtual might be useful, but I can''t think of it at the moment.

Of course we don''t live in an ideal world, and making functions virtual does have a cost. Off the top of my head virtual function overhead comes in the following ways:
1) increased number of instructions in order to call a virtual function. mov and call on an x86 as compared to just a call, in addition it has a double indirection on that, which can make a difference on pipeline stalls and cache misses. This takes up additional clock cycles as well as increases the executable size (i.e. also fouls up the instruction cache.)
2) loads vtable which pollutes the data cache. (as compared to a non-virtual call)
3) increases number and size of vtables. (increases executable size, wastes memory)
4) opportunity cost in register allocation as vtable index is loaded into a register.

Combined, these make very good reasons not to put virtual function calls in a tight loop. Most other cases you probably wouldn''t notice it, but call a virtual pixel plotting function in your blitting routine, and you''re going to have trouble.

Declaring variables private, is good if you know that it would be a design flaw for a child class to mess with a particular variable. Ex: you have a variable that counts the number of times one of two functions are called. You don''t want any child classes modifying the count, so you can declare that variable private.

Share this post


Link to post
Share on other sites
Cool. Thanks SiCrane.

Btw, since you said that you can't think of cases where a static function will be useful (other than in performance critical areas), does that mean that you use virtual functions a lot? Or do you use static functions most of the time and just use virtual functions when you expect the child class to override the parent's implementation of the function?

Regarding private/protected, understood. Thanx.


Philip



Edited by - NuffSaid on November 30, 2000 10:08:24 AM

Share this post


Link to post
Share on other sites
Mrs Bennidict says that it is on the agenda!

God''s last message to creation:
"Sorry for the inconvinience"
"So long and thanks for all the fish" -Dolphines

Share this post


Link to post
Share on other sites
I believe you misunderstand me with regards to static and virtual member functions. A static member function is not the same as a standard member function. i.e. A static member function is a function that belongs to the class and is not bound to a particular instance of the class. A standard member function will be called according to the type of the pointer used to indicate the object. A virtual function is a member funciton that when called off a pointer is guaranteed to call the overrided function of a class no matter what type of pointer is used to indicate the object.

Because there is that overhead, if a function does not need to be virtual, I use a standard member function. Does that clear things up?

Share this post


Link to post
Share on other sites
Sorry, my mistake.

Yeah, that did clear things up a lot. I forgot that you could add the static keyword in front of a function and make it a static function (which was what you were referring to).

Thanks


Phil

Share this post


Link to post
Share on other sites
quote:

if performance wasn''t a problem, would it be a good idea to make all functions virtual?



Nope, the only time you should make a function virtual (even if there is no speed cost) is when you want subclasses to be able to provide their own implementation of the function. In some classes that might be all functions (although not the ones used in a constructor or destructor), but not in all classes.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!