What is virtual keyword and how to implement it in interface using Visual Studio
I would like to know what is virtual keyword in Visual Studio C++? How to implement it in interface? I recently saw some code like this:
Code:
class A
{
public:
virtual ~A();
}
class B : public A
{
public:
virtual ~B();
}
class C: public B
{
public:
virtual ~C();
}
I was just thinking why the destructor over here is declared as virtual. Was that required?
Re: What is virtual keyword and how to implement it in interface using Visual Studio
A base class method is defined as "virtual" if we want it to be overriden. However if you want to avoid this then use "sealed" keyword. You can declare a class method "virtual" but not a class member. A class member may be overriden even though the virtual keyword is used. You may however use "override" keyword to override virtual method.
Re: What is virtual keyword and how to implement it in interface using Visual Studio
Just to summarize some points to what opaper said:
- virtual is used when a method is to be defined as virtual.
- override is used when a method has to override a virtual method.
- abstract is used with virtual (or override) when a method has to be defined without implementation.
- final is used with virtual or override to force a method not to be overridden.
Re: What is virtual keyword and how to implement it in interface using Visual Studio
Visual Studio C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. A virtual function is a member function of a class and is declared with virtual keyword. A virtual function has a different functionality in the derived class and it is resolved at run-time.