Solved

C++ Virtual Functions

Posted on 2004-09-13
4
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

630 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