?
Solved

Virtual method/ virtual destructor: two questions

Posted on 2013-01-22
4
Medium Priority
?
378 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 675 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 675 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:Deepu Abraham
Deepu Abraham earned 150 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

840 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