Solved

Virtual method/ virtual destructor: two questions

Posted on 2013-01-22
4
336 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
  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 …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

18 Experts available now in Live!

Get 1:1 Help Now