Solved

Virtual method/ virtual destructor: two questions

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

770 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