+ 2
What are virtual functions in C++?
What are virtual functions, and what are "pure" virtual functions? I've seen people mention them before, and I've been curious.
5 Réponses
+ 4
Virtual functions are used in polymorphism ( ability to change at runtime ).
If a function is marked with virtual it means that a derived class has the ability to override its functionality.
Example:
#include <iostream>
class Base
{
public:
virtual ~Base(){};
virtual void Foo(){ std::cout << "Base\n"; }
};
class DerivedA : public Base
{
public:
// Without virtual in Base::Foo you wouldn't be able to override
void Foo() override { std::cout << "Derived A\n"; }
};
class DerivedB : public Base
{
public:
void Foo() override { std::cout << "Derived B\n"; }
};
int main()
{
int c;
std::cin >> c;
Base* b = nullptr;
if( c == 0 ) b = new DerivedA;
else b = new DerivedB;
b->Foo();
delete b;
}
If the user enters 0 then DerivedA will be instantiated and the output will be "Derived A".
Any other value will instantiate DerivedB and the output will be "Derived B".
In this example it is possible to still do something like 'Base* b = new Base;'
Sometimes you don't want the base class to be instantiatable and that is where pure virtual functions come in.
A pure virtual function is a virtual function without a body, marked with '= 0;' instead of '{}'
If a class has even 1 pure virtual function like 'virtual void Foo() = 0;'
then the entire class cannot be instantiated.
Therefore 'Base* b = new Base;' will be illegal.
Also any class that can be used in polymorphism ( a class with at least 1 virtual function ) must have a virtual destructor ( won't result in an error if you don't ), otherwise your compiler will not be able to destruct the objects correctly. These 'failed' destructions are also called 'zombie objects'; they are not alive but also not quite dead.
+ 4
Dennis
1.
I noticed you mentioned virtual destructors
Are there virtual constructors?
2.
Why would one want to be unable to instantiate a class?
3.
Do virtual functions have uses outside of classes and objects?
+ 3
1.
A constructor cannot be virtual. It's not even inherited.
When a derived class is constructed it also calls the constructor of the base class. If it could be virtual then the derived class would be able to override it and then the construction of the base class would fail.
A destructor is virtual so that it is added to the virtual jump table, which allows polymorphic objects to call the correct destructor. The destructor is a bit special here. You cannot 'override' it, they are still called in reverse order.
2.
Sometimes classes with virtual functions' only purpose is to be an interface/abstract ( like the interface keyword in C# ).
They only tell that certain functions must exist in a class when derived from.
Constructing them would make no sense because they have zero functionality.
3.
No, virtual functions cannot be used outside classes.
+ 3
An interface is just a class with declarations of virtual functions but no definitions, other classes that inherit it must implement them.
This is an inferface:
class Clone
{
public:
virtual ~Clone(){}
virtual void* CreateClone() = 0;
};
Now you know that any object that inherits Clone must have an implementation for CreateClone.
+ 2
One last question
What exactly is an interface?