Solved

C++ Virtual Functions

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

914 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now