why is it possible to make the destructor pure virtual [duplicate] - c++

This question already has answers here:
Why do we need a pure virtual destructor in C++?
(11 answers)
Closed 7 years ago.
As I know in cpp, when we delete an object or when the main finishes, the destructors of all objects will be called. For those objects whose type are class child, the destructors of class child will be called first then the distructors of class parent will be called.
Now I am confused. Because if a pure virtual destructor is allowed, how could it be called when we delete an object of class child? Doesn't it call the destructor of class parent which is pure virtual?

Yes, the destructor of the base class is called. This means it must have an implementation. It is possible to provide an implementation for any pure virtual function, including the destructor. For example:
struct foo
{
virtual ~foo() = 0; // pure virtual dtor
};
foo::~foo() {} // implementation
The use-case of a pure virtual destructor is to ensure a class without any other pure virtual methods cannot be instantiated.

Related

Use of Virtual Destructor in C++ [duplicate]

This question already has answers here:
Why do we need a pure virtual destructor in C++?
(11 answers)
Closed 2 years ago.
What is the application of virtual and pure virtual destructor in C++? What is the scenario where I would have to use a virtual destructor instead of a normal destructor?
When a pointer to a base class object is deleted, the compiler calls the corresponding destructor based on the actual type of object the pointer refers to.
If the base class destructor is not a virtual function, the compiler will automatically call the destructor of the base class when the base class pointer to the derived class object is deleted, without considering whether the actual object is an object of the base class.This can lead to memory leaks.

C++. Calling a virtual member function in destructor [duplicate]

This question already has answers here:
Calling virtual function from destructor
(5 answers)
Closed 7 years ago.
Every class that gets extended with this calls abort in the destructor and the call stack tells me that the function that called abort was called from a unreasonable spot in the header file. The other functions get overriden and work fine though.
Renderable.h
#pragma once
class Renderable
{
public:
virtual void update(long delta) = 0;
virtual void destroy() = 0;
virtual void render() = 0;
virtual ~Renderable();
};
Renderable.cpp
#include "Renderable.h"
Renderable::~Renderable()
{
(this->*(&Renderable::destroy))(); // GLUtils::runOnUIThreadWithContext(this, &Renderable::destroy, true);
} // It says that it gets called from here.
When instantiating an object the base class gets initialized and then the subclass gets initialized. When destructing an object the subclass gets destructed and then the base class. After the subclass is destructed, its members and virtual methods are unavailable—there is no destroy() method to be invoked. I suggest you move the logic in the destroy() method to the subclass destructor.

overriding virtual function to non-virtual function is OK? [duplicate]

This question already has answers here:
C++ "virtual" keyword for functions in derived classes. Is it necessary?
(9 answers)
Closed 9 years ago.
class Base
{
virtual void foo();
}
class Derived : public Base
{
void foo();
}
It it OK?
Or it can make some problems?
I think it insist "DO NOT INHERIT FROM DERIVED".
Once you marked the foo() function in Base class as virtual it is implicitly virtual in Derived class, even if you don't mention the keyword virtual in front of it.
virtualness is inherited. Even if you do not declare an overridden method virtual, it will be virtual.
Hence, if you access an object of Derived using Base pointer or reference, it will call foo of Derived.
virtual functions are inherited automatically. So even if you don't declare it as virtual, it is virtual actually.
The reason for you to explicitly declare it as virtual is for better clarification so that anyone reading this code can instantly understand that it is a virtual functions, without having to check the base class declarations.

Calling pure virtual function in constructor gives an error [duplicate]

This question already has answers here:
call to pure virtual function from base class constructor
(8 answers)
Closed 5 years ago.
class a //my base class
{
public:
a()
{
foo();
}
virtual void foo() = 0;
};
class b : public a
{
public:
void foo()
{
}
};
int main()
{
b obj; //ERROR: undefined reference to a::foo()
}
Why it gives me error? The pure virtual foo is defined. What do I need to change in my code to make it work? I need pure virtual method from base class be called in its constructor.
Calling virtual functions in a constructor is recognised as a bad thing to do.
During base class construction of a derived class object, the type of
the object is that of the base class. Not only do virtual functions
resolve to the base class, but the parts of the language using runtime
type information (e.g., dynamic_cast (see Item 27) and typeid) treat
the object as a base class type.
So your instantiation of b invokes the a constructor. That calls foo(), but it's the foo() on a that gets called. And that (of course) is undefined.
Quoted from a book "Let Us C++"by Yashwant Kanetkar
It is always the member function of the current class , is called.That
is , the virtual mechanism doesn't work within the constructor
So, the foo() of class a gets called.
Since it is declared pure virtual, it will report an error
foo function is called in class a's constructor, and at that time, object b has not been fully constructed yet, hence it's foo implementation is unavailable.
Quoted from "Effective C++":
Don't call virtual functions during construction or destruction,
because such calls will never go to a more derived class than that of
the currently executing constructor or destructor

When the destructor of a subclass is called, will the destructor of a parent class also be called? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Do I need to explicitly call the base virtual destructor?
Lets say you have the following:
class ParentClass {
...
virtual ~ParentClass();
and
class ChildClass {
...
virtual ~ChildClass();
Which of the destructors would be called? Would both the parent and child's destructors be called? Currently don't have C++ compilers set up on my computer.
If ChildClass is derived from ParentClass then the derived destructor is called first, followed by the parent class. As it stands in your code, ChildClass does not inherit from ParentClass
Yes, both constructors are call: construction and destruction are symmetric: All subobjects get destroyed in exactly the opposite order they were created. For the order of destruction it doesn't matter if the destructor is virtual. The only impact of virtual vs. non-virtual destructors is when deleteing an object of a dreived type using a pointer to a base: This results in undefined behavior if the destructor of the base isn't virtual.