Solved

C++ Virtual Functions

Posted on 2004-09-13
4
250 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

771 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

12 Experts available now in Live!

Get 1:1 Help Now