Solved

C++ Virtual Functions

Posted on 2004-09-13
4
252 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

776 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