Solved

C++ Virtual Functions

Posted on 2004-09-13
4
253 Views
Last Modified: 2010-04-01
I am experienced in C and somewhat in JAVA.  I am now working in C++.  In JAVA, I know that all methods are virtual and that in C++ they are only virtual if you declare them that way. . .

Is it a good or bad idea to declare all C++ methods as virtual?

I am looking for a FEW opinions here.  So, please tell me why as well.  I think I know what goes on behind the scenes in virtual vs. non-virtual functions.  (I will most likely be writing C++ code to work with or in embedded projects.)

Thank You!
0
Comment
Question by:klopex
  • 2
4 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12043510
>>Is it a good or bad idea to declare all C++ methods as virtual?

It's a bad idea.  You loose performs.

In most cases, you don't need a virtual function.

0
 
LVL 30

Accepted Solution

by:
Axter earned 50 total points
ID: 12043555
FYI:
If you look at the C++ STL classes (std::string, std::iostream, std::vector, etc...), you'll notice that none of them are virtual.
This was done because they did not want loose optimization, and therefore reduce performance.

If you think your class will get inherited, then make a function virtual.
Otherwise, leave it non-virtual.

Even if your class will get inherited, you still don't necessarily want to make all your functions virtual.  For example, if you have private member functions, it makes no sense to have them virtual, sense descendant class can’t call them.
0
 
LVL 3

Author Comment

by:klopex
ID: 12043817
Thank You Axter.  I had supposed that what you are saying was true.  I figured that there was a voice of experience that I should listen to instead of my own suspicions!  I work in an Electrical Engineering environment, so they don't always offer me a strong confidence when they answer such Comp. Sci. oriented questions.  Often, it seems like they are making up the answer as they go.  When I asked this around here, they answered, "No, (pause) I don't (pause) think you need to make them all virtual."  I figure they know their stuff well, but I don't want to start my project in a foolish position!!!

I haven't considered making private member functions non-virtual.  Thanks.  I suppose I would realize that when I find myself in that position.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12044096
Is it a good or bad idea to declare all C++ methods as virtual?

I fully agree to all that Axter had told you.

Virtual functions should be made very carefully as you couldn't easily change the interface once defined. Also the argument list is limited as it must apply to all potential descendant classes (and not only the first ones you defined yourself). Also public baseclass member functions must not made virtual if the baseclass implementation can be made complete, e. g.

class Base
{
private:
       int x;
       int y;
public:
       int getProduct() { return x*y; }   // non-virtual
};

Regards, Alex
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question