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.
249
questions
1
vote
1
answer
87
views
Vector of polymorphic objects and unique_ptr
I am trying to do an assignment and decided to properly manage memory this time but ended up with more questions than answers. I want to have a vector of children of a parent class for example vector&...
0
votes
0
answers
80
views
Why does virtual destructor cause segmentaion fault when dealing with user-defined MPI types?
I'm using the example provided with Boost.MPI documentation found here. I have a bit modified these two classes and as soon as I add destructor to this class, I will get segmentation fault during ...
1
vote
2
answers
164
views
Why does dynamic_cast need the base class to have a virtual destructor? [duplicate]
I had the following code, on which I tried doing a dynamic_cast:
struct Base {
Base() {}
~Base() {}
};
struct Derived: public Base {
Derived() {}
~Derived() {}
};
struct ...
0
votes
0
answers
240
views
Resolving "undefined reference" on pure virtual destructor of base class [duplicate]
I am learning Object Oriented programming. Here is a simple code snippet that I have written:
class Food {
protected:
int id;
FoodTypes type;
string name;
public:
Food(int id, ...
-1
votes
1
answer
68
views
I don't understand C++ Primer explanation about virtual destructor
I don't understand this phrase from the famous C++ Primer :
"classes used as the root of an inheritance hierarchy almost always
define a virtual destructor."
I don't understand why the ...
10
votes
1
answer
376
views
Problem with virtual destructor when using templated operator delete
The following class (with virtual destructor) contains a templated operator delete:
struct S
{
virtual ~S() {}
template <typename... Args>
void operator delete(void* ptr, Args... ...
0
votes
1
answer
77
views
C++ Memory Layout: Questions about multiple inheritance, virtual destructors, and virtual function tables
I have a main.cpp file as follows.
#include <stdio.h>
class Base1
{
public:
int ibase1;
Base1() : ibase1(10) {}
virtual void f_b1_1() { printf("Base1::f_b1_1()()\n"); }
...
3
votes
3
answers
146
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
191
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
98
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
183
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
206
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
222
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
122
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
129
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
59
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
463
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
109
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
288
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
124
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 << &...
3
votes
1
answer
538
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
52
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
108
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 ...
2
votes
2
answers
792
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
237
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
126
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
416
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
54
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
95
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" << ...
3
votes
1
answer
370
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
455
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
48
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
62
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
172
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(...
3
votes
1
answer
276
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
351
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
820
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
150
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
445
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
531
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
2
answers
384
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
2k
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) ...
5
votes
7
answers
3k
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 ...
3
votes
1
answer
413
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
890
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
64
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
101
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
541
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
372
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 ...