destructor Questions

2

Solved

Can someone explain the execution order of this code? struct Foo { ~Foo() { std::cout << "1"; } }; int main() { const Foo& bar = Foo(); const Foo& baz = std::move(Foo(...
Tension asked 7/2, 2023 at 10:56

5

Solved

Suppose that I have the following (trimmed down) code: class P { P(); P(const P&); ~P(); } void foo(P x) { ... } void bar() { P p{}; foo(p); // compiler uses P::(const P&) to construc...
Owades asked 13/6, 2019 at 15:44

3

Solved

Is there a way to determine if python is shutting down? Basically: def Foo(object): def __del__(self): if PYTHON_IS_EXITING: do_this else: do_that foo1 = Foo() del foo1 # calls do_that foo2...
Violetteviolin asked 21/3, 2014 at 20:29

5

I have a C (nested) structure that I would like to automagically initialize and destroy in my code. I am compiling with GCC (4.4.3) on Linux. I am vaguely aware of GCC function attributes construc...
Tearjerker asked 7/1, 2012 at 11:58

8

For example: public class Person { public Person() { } ~Person() { } } When should I manually create a destructor? When have you needed to create a destructor?
Godchild asked 4/2, 2011 at 13:55

2

I get segmentation fault exception on the next code: class A { public: A() {} virtual ~A(){} double m_d; }; class B : public A { public: B() {} virtual ~B(){} int x; }; int main() { A*...
Unswerving asked 22/11, 2022 at 19:7

2

I have written a simple program to test "vector.erase" feature. There is a simple class (MyClass0) which writes some related message in it's constructor and another in it's destructor. An...
Genus asked 17/11, 2022 at 13:16

3

In an std::vector<T> the vector owns the allocated storage and it constructs Ts and destructs Ts. Regardless of T's class hierarchy, std::vector<T> knows that it has only created a T an...
Gurl asked 6/8, 2022 at 6:48

11

Solved

class Package: def __init__(self): self.files = [] # ... def __del__(self): for file in self.files: os.unlink(file) __del__(self) above fails with an AttributeError exception. I understan...
Baliol asked 14/5, 2009 at 19:4

7

Solved

From Python documentation: It is not guaranteed that __del__() methods are called for objects that still exist when the interpreter exits. As far as I understand, there is also no way to guara...
Underrate asked 28/2, 2020 at 9:24

3

Solved

I have this code and this outputs the following: link to the following example https://godbolt.org/z/z8Pn9GsTv template <typename T> struct A1 { A1() { std::cout << "construction...
Erupt asked 17/5, 2022 at 6:50

10

Solved

I know the interface is working. When I started coding in my project, I got this doubt in my mind. Can anyone clarify ?
Aether asked 29/8, 2011 at 5:40

4

Solved

I have a class that is using RAII for cleanup in case something goes wrong. This means the class contains a flag, that tells it whether the work has been completed, and if this flag is not set when...
Leper asked 1/4, 2011 at 9:34

4

Solved

I have 2 files: Point.h: class Point { int x; int y; char* name; public: Point() { name = new char[5]; } ~Point() { delete[] name; } }; and: Line.h: class Point; class Line { Point* p; ...
Doone asked 4/4, 2013 at 22:4

3

Solved

Earlier today I asked this question about the __del__() method of an object which uses an imported module. The problem was that __del__() wants to make use of the module os, but sometimes (not alwa...
Sheppard asked 20/2, 2013 at 17:49

3

Solved

Is there destructor in TypeScript? If not, how can I delete an object? I tried destructor() and ~ClassName() but it didn't work.
Fredericton asked 5/3, 2015 at 19:1

6

Solved

I am creating a class in which I want to generate a temporary workspace of folders that will persist for the life of the object and then be removed. I am using tempfile.mkdtemp() in the def __init_...
Gammadion asked 14/11, 2012 at 13:29

5

Solved

Consider the following code: void foo() { { CSomeClass bar; // Some code here... goto label; // and here... } label: // and here... } Will the destructor of bar be called ?
Charters asked 5/7, 2010 at 13:41

12

Solved

Please give me some real life examples when you had to use __destruct in your classes.
Ohmage asked 25/8, 2010 at 13:13

1

Solved

The code as follows struct B { ~B() = delete; }; B * b = new B{}; fails to compile in the latest MSVC with the error: error C2512: 'B': no appropriate default constructor available note: Invalid...

8

Solved

Can the default destructor be generated as a virtual destructor automatically? If I define a base class but no default destructor, is there a default virtual destructor generated automatically?
Camfort asked 13/7, 2009 at 2:3

1

Solved

Consider the following code: #include <cstdlib> struct Foo { ~Foo() { std::exit(0); } } foo; int main() { } It compiles and terminates with zero successfully for me both on my Linux (GCC,...
Cornelius asked 27/10, 2021 at 11:37

6

Solved

The C++ standard says that invoking a pure virtual function from a constructor or destructor is forbidden. What is the reason for this? Why should the standard place a restriction like this?
Tolkan asked 28/12, 2011 at 4:36

1

Solved

Why are imported modules not available in Python destructor (__del__ method)? Can it be made available via some way? For example, the following fails when invoking __del__ during interpreter shutdo...
Flamethrower asked 15/9, 2021 at 12:39

2

Solved

The following code compiles in gcc 9.1 godbolt but not clang 8 godbolt: class A { protected: ~A() = default; }; class B final : public A { }; int main() { auto b = B{}; } Clang's error: &lt...

© 2022 - 2025 — McMap. All rights reserved.