Questions tagged [virtual-destructor]
A virtual destructor ensures a C++ object will correctly call the destructor of the most-derived class when a polymorphic object is deleted through a pointer to its base class.
241
questions
3
votes
3
answers
87
views
How is the size of a polymorphic object deduced in a delete operation?
I know that there are a lot of similar questions on SO related to what I am about to ask, I've read many of them and still feel a bit vague, so I decided to ask this question.
Given the following code:...
0
votes
1
answer
75
views
Destructor protected within this context c++ (issue #1) [duplicate]
I have a compilation error with C++11.
I have defined a base class Node with protected ctor and dtor. I have defined an inherited class Directory with public inheritance.
I am calling the dtor of the ...
1
vote
2
answers
69
views
C++: Implementation of virtual destructor necessary when using inherited structs with only properties?
I know that I need to define a virtual destructor (best option even if my class is final).
In my case, I am using C-like structures (no functions, no defaults, just plain members) and use inheritance ...
1
vote
1
answer
90
views
A misunderstanding with polymorphism and virtual destructors
Example:
class Base {
public:
virtual void f() = 0;
virtual ~Base() { std::cout << "Base::~Base()\n"; }
};
class Derived : public Base {
public:
void f() { }
~...
2
votes
1
answer
134
views
Is the destructor of a derived class virtual by-default if the base class destructor is virtual?
I was recently reading about virtual functions and virtual destructors, and the following question aroused.
For instance, I have the following inheritance chain.
class Base
{
public:
virtual ~Base(...
0
votes
1
answer
135
views
Intel C++ compiler and Wnon-virtual-dtor flag gives (wrong?) warning
I was trying to compile some code of mine which, in g++ (with the --Wnon-virtual-dtor Flag) compiled just fine. Also, my IDE and clang-tidy didn't warn me (I see that this might be false of course).
...
3
votes
1
answer
106
views
Why does a virtual destructor write to memory?
Recently, when working with custom allocator code and placement new+delete, I noticed something that surprised me: When a virtual destructor is called, it writes to the object's soon-to-be-freed ...
1
vote
1
answer
96
views
C++: Can the destructor be the only virtual function in a class?
I read that the destructor should be declared virtual only if there is virtual function in a class.
Is this a requirement or is it possible to do otherwise?
1
vote
1
answer
47
views
Deriving from an abstract base with a user-declared dtor and move-support
A user-declared dtor prevents the autogeneration of the move-ctor/-assignment-operator,
but will the autogeneration only be prevented in the class where the dtor has been defined or will the ...
-1
votes
1
answer
181
views
deleted function 'virtual Classname::~Classname()' overriding non-deleted function VIRTUAL DESTRUCTOR
I am trying to find the problem in this code, it does not compile in C++14.
The original problem was to make this compile 'without editing Object or modifying drawObject()'s prototype':
#include <...
0
votes
2
answers
102
views
Inheritance in c++ : destructors
I added a protected member array in the abstract base class that each derived class uses, should the destructor of the base class be virtual or can I do:
**~base(){
delete[] array;
}**
0
votes
1
answer
233
views
segmentation fault with large number of shared_ptr
this I is my .h file:
class Node
{
public:
static void disp(const std::vector<int> &v);
static size_t Node_no;
Node(const std::vector<int> &...
0
votes
1
answer
82
views
Virtual destructor, what would happen I didnt have a destructor in the derived class?
I've just had my lesson about virtual destructors and I have a question.
Lets say we have this code below:
#include <iostream>
class Base {
public:
virtual void fun() { std::cout << &...
2
votes
1
answer
173
views
deleting object through pointer to base without virtual destructor
I have the code:
class A1 {
public:
A1() { cout << "A1"; }
virtual ~A1() { cout << "~A1"; }
};
class A2 {
public:
A2() { cout << "A2"; }
...
2
votes
0
answers
50
views
Inline destructor "causes" bad_alloc [duplicate]
I have encountered a strange bug in my program which was solved by making a destructor non-inline:
File a.h
class A {
...
inline virtual ~A(){}
};
File b.h
class B : public A {
...
...
1
vote
1
answer
79
views
Is there any advantage if virtual destructor is not defined with shared_ptr
When we use shared_ptr with polymorphic classes we don't need virtual destructors due to type erased deleter.
But does it makes sense to define a destructor in simple cases.
What can be the advantages ...
1
vote
2
answers
379
views
Using enable_shared_from_this in polymorphic inheritance with virtual destructor
I have the following class structure for Managing callbacks with different prototypes:
class MethodHandlerBase: public std::enable_shared_from_this<MethodHandlerBase>{
public:
virtual void ...
2
votes
1
answer
142
views
std::enable_shared_from_this, non-virtual destructor and public inheritance
The std::enable_shared_from_this class is a (template) mixin, recommended for use to enable creating shared pointers from a given object (or its address), which all have common ownership of the object....
0
votes
1
answer
71
views
C++ primer 5 ed. Virtual destructor and move semantics
In C++ primer 5 ed. it is said:
"The fact that a base class needs a virtual destructor has an important indirect impact on the definition of base and derived classes: If a class defines a ...
2
votes
2
answers
193
views
How to derive from a class without virtual-destructor?
There is a virtual class as a callback interface, that I can neither modify, nor ask the author to fix. The only members of the class are a lot of virtual methods that can be overridden, so as to let ...
0
votes
0
answers
52
views
How can a destructor be both virtual and inline in C++? [duplicate]
The general good practice is that trivial constructors, destructors, get/set functions, etc, can be directly defined inside a class definition in a header file, rather than providing their bodies in a ...
0
votes
1
answer
84
views
C++: What is the order of destructor call with methods?
I got this code:
Edit: The full code:
#include <iostream>
using namespace std;
class A {
public:
A() {}
A(const A& a) {
cout << "A copy ctor" << ...
1
vote
0
answers
146
views
Base class virtual destructor - rule of five?
I have a base State interface class with virtual default destructor.
class State {
public:
virtual void event() = 0;
virtual ~State() = default; // relevant part
virtual void onCreate() ...
2
votes
2
answers
112
views
How to prevent an object from being deleted via a pointer to its parent type?
struct A
{};
struct B : A
{
operator A() const = delete; // not work
};
int main()
{
B* p_derived = new B();
delete p_derived; // ok
// How to make the following two lines illegal?
...
0
votes
0
answers
41
views
Why the inheritance of a class with non-virtual destructor is not a good thing even if the derived class adds no members?
In this answer answer:
In particular, you are not allowed to delete a std::vector<T>* that
actually points at a derived object (even if the derived class adds no
members), yet the ...
0
votes
2
answers
59
views
How can be that a destructor is called but no constuctor before that?
I am currently learning the object oriented feature of c++. I wrote a piece of code to test how inheritance and polymorphism works.
Here is a part of the code:
class Person
{
public:
...
2
votes
1
answer
139
views
C++ inheritance and valgrind memory leak
I was checking my codes with valgrind and it found a memory leak. I didn't understand why it happened. Instead of putting my main code, I made a similar program to check whether my other allocations(...
2
votes
1
answer
159
views
How can I aggregate init a struct that inherits from a virtual type?
As per [dcl.init.aggr] I cannot aggregate init a type, if it has (among other things) virtual functions, which includes inheriting from a type with a virtual destructor. However, I'd like to avoid ...
2
votes
1
answer
203
views
Why does the deleting destructor occupy a second vtable slot besides the ordinary destructor?
In C++ ABI implementations modeled after the Itanium C++ ABI, which is followed by many ABIs for other processors, virtual destructors actually occupy two vtable slots. Besides the "complete object ...
0
votes
5
answers
456
views
Overload -> arrow operator in shared_ptr<interface> instance with no pure virtual destructor in interface
I'm trying to overload the -> operator to eventually execute something along the lines:
MyInterface *myInstance = (MyInterface *)(new A());
myInstance->Toggle(); //this works wonderfully
std::...
2
votes
1
answer
80
views
Defaulted destructor in base class disable move constructor in child class if there is a member
Why does defaulted (user declared) destructor in Base1 prevent generation of move constructor/operator in Child1 class, but everything work fine when I move member data from Base (Base2) to Child (...
1
vote
3
answers
312
views
Order of calling virtual destructors in C++
Well so i have been trying to understand OOP concepts through C++ , however i am not able to get some parts of virtual destructors.
I have written a small snippet :
class A{
int x;
public:
...
0
votes
1
answer
231
views
correct usage of std::vector in dll interfaces (class with virtual destructor!)
I will try to explain my issue with an example.
I have the following class:
__declspec(dllexport) class myclass
{
public:
int a;
int b;
myclass() {};
virtual ~myclass() {};
// ~...
7
votes
1
answer
244
views
Does the standard allow an implicit virtual destructor not being implicitly defined when no instances of its class are created?
While thinking about this question, I stumbled upon something else I don't understand.
Standard says...
[class.dtor]/4
If a class has no user-declared destructor, a destructor is ...
4
votes
3
answers
1k
views
How to properly implement a C++ class destructor
In a class (without direct pointer members), I see there are following 3 possibilities for defining a destructor.
class Child : public Parent
{
public:
// ~Child() override {} // (1) ...
4
votes
7
answers
2k
views
virtual destructor for pure abstract class [duplicate]
Based on what I found here and on other links on stackoverflow, we should always define a virtual destructor in the base class if we plan to use it polymorphically. I want to know if there is an ...
2
votes
1
answer
238
views
Why does a virtual destructor require operator delete?
Consider the following code:
class Base {
public:
#ifdef __VIRTUAL__
virtual ~Base() {}
#else
~Base() {}
#endif
};
class Derived : public Base {
public:
~Derived() {}
private:
static ...
0
votes
0
answers
65
views
Practicality of virtual destructor in Base Class [duplicate]
Lets take this example:
/* Source.cpp */
struct Base {
virtual void func() = 0;
};
struct Derived : public Base {
virtual void func() override { }
};
int ...
0
votes
0
answers
419
views
If I have defined a virtual destructor in the class declaration, what code is required in the implementation (.cpp) file?
I have been provided with a header file that includes a base class declaration. Within the class declaration is a virtual destructor that has been defined. From this, we need to code the ...
1
vote
2
answers
62
views
How to get virtual destructors to be called in C++?
I am trying to see the effects of calling virtual destructors of classes belonging to a long chain of hierarchy: class A to class E.
Strangely, the destructors do not write anything to the console. I ...
0
votes
1
answer
78
views
Can a base destructor really be overridden by a derived destructor in C++?
All over the web, as well as in Bjarne Stroustrup's C++ book, I see statements like, "If a base destructor is declared virtual then it is overridden by a derived class's destructor."
But, why is it ...
0
votes
1
answer
394
views
Virtual destructor for pure abstract base classes
I have seen here and here that a good rule of thumb is to use virtual destructors for every class that is intended as a base class. I have a pure abstract base class (only contains pure virtual ...
1
vote
1
answer
298
views
Do I need virtual destructors in a world without dynamic memory?
Virtual destructors are needed when an object is (potentially) destructed from a base class pointer.
Consider a program without dynamic memory as often found in embedded systems. Here, using new or ...
2
votes
2
answers
836
views
What do I Need to Return an Object with a unique_ptr Member?
Let's say that I have this object:
struct foo {
std::unique_ptr<int> mem;
virtual ~foo() = default;
};
I can no longer return a foo object created in a function:
foo make_foo() {
...
23
votes
1
answer
3k
views
Valgrind shows memory leak in std::make_unique
I'm using Valgrind to check for memory leaks.
Unfortunately I get a Leak_DefinitelyLost warning.
Attached is a simplified version of my code that reproduces the error:
#include <iostream>
#...
0
votes
1
answer
373
views
C++ slicing and virtual destructors
I get a vector of raw pointers, which I copy. The ownership of the objects pointed by these pointers belong to some other module. I have to replace some of the pointers with new ones which point to ...
4
votes
2
answers
141
views
virtual destrutors vs normal methods in C++
Consider the following three programs in C++:
program 1
struct base{
virtual ~base() =0;
};
struct derived: public base{
~derived();
};
derived::~derived(){}
int main(){}
program 2
struct ...
0
votes
1
answer
226
views
Why fdump-class-hierarchy gives two pointers int vtable for virtual functions
for the following class,
class A
{
public:
char VarA;
int VarB;
virtual ~A(){}
};
g++ fdump-class-hierarchy gives me for the Vtable,
Vtable for A
A::_ZTV1A: 4u entries
0 (int (*)(....
21
votes
3
answers
3k
views
Must a c++ interface obey the rule of five?
What is the correct way to declare instantiation methods when defining an interface class?
Abstract base classes are required to have a virtual destructor for obvious reasons. However, the following ...
0
votes
1
answer
28
views
Linker complains about lack of implementation for pure virtual destructor for base abstract class
Something that I learned from this question, but that I thought I'd give a more detailed and easily searchable answer to, is that if you have a base abstract class meant to be inherited such as this, ...