[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Virtual method/ virtual destructor: two questions

Posted on 2013-01-22
4
Medium Priority
?
368 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
[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 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

  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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

656 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