Solved

Virtual method/ virtual destructor: two questions

Posted on 2013-01-22
4
344 Views
Last Modified: 2013-01-23
Hello experts,

two questions if I may:

- why virtual method is called virtual? What is so virtual about it?

- why exactly once we make a method virtual we need to create destructor and why it needs to be virtual?

Thank you

panJames
0
Comment
Question by:panJames
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 225 total points
ID: 38806528
Well, it is called 'virtual' because 'in object-oriented programming, a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature.' (saving myself the typing, see http://en.wikipedia.org/wiki/Virtual_function) by using this very keywork when the method/function. is declared

Regarding 'virtual destructors': If a class has a public virtual destructor, it can be derived from, and the derived object can be safely deleted through a pointer to the base object (see http://www.parashift.com/c++-faq-lite/virtual-dtors.html) - the calling order of the base class' destructors is guarantedd.
0
 
LVL 22

Assisted Solution

by:ambience
ambience earned 225 total points
ID: 38807147
>> why exactly once we make a method virtual we need to create destructor and why it needs to be virtual?

You do not - theres no such restriction. You do not need to create a destructor and even if you do theres no requirement for it to be virtual.
0
 
LVL 22

Expert Comment

by:ambience
ID: 38807182
Thinking about it, when you add a virtual method to a class you are actually considering that it would act as a base class and expect derivations. Also, it suggests that the intention might be polymorphic behavior - utilizing through the base pointer.

Given these assumptions, it is safe to ** expect ** that some derivation may actually require to allocate resources and therefore a mandatory destructor. However, unless there is a destructor in the base and its virtual derivations would not be safely usable in a polymorphic way.

So its not a requirement but more of a good practice that if omitted opens the potential for running into future problems.
0
 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 50 total points
ID: 38808892
In a nutshell,

The keyword virtual means that in the run-time system will automatically invoke the proper member function when it has been overridden by a derived class.
In short, virtual means overridable.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Focus not getting shifted out of  editbox 2 66
max float value 3 51
Why isn't object file created? 6 68
Adding items to a C# list incrementally 5 30
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

839 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