explicit-destructor-call Questions

4

I stumbled upon the following code snippet: #include <iostream> #include <string> using namespace std; class First { string *s; public: First() { s = new string("Text");} ~First() ...

2

Solved

In The C++ programming language Edition 4 there is an example of a vector implementation, see relevant code at the end of the message. uninitialized_move() initializes new T objects into the new m...

1

Solved

For ordinary objects (even for const ones), it is permissible to end their lifetime by explicitly calling the destructor. Later, for example, the program can start another object lifetime in the sa...
Levitan asked 23/8, 2021 at 5:15

1

Solved

is it required by the standard to call non-trivial destructor when you know that in this specific case the destructor is a noop ? is the code likely to be broken by compliers if the destructor is ...

2

Solved

In my constructor, I have to destroy any remaining resources if any code in it throws. I'd like to avoid writing duplicate code so I just call the destructor in the catch block which than frees any...

3

Solved

I have class Data which can hold a pointer to an object. I want to be able to call its destructor manually later on, for which I need its address stored in a variable but it seems that taking the a...
Helicline asked 10/10, 2018 at 9:54

3

Solved

In the following C++ code, I am allowed to explicitly call the destructor but not the constructor. Why is that? Wouldn't be explicit ctor call more expressive and unified with the dtor case? class...

2

Solved

I know that calling destructor explicitly can lead to undefined behavior because of double destructor calling, like here: #include <vector> int main() { std::vector<int> foo(10); fo...

1

I just wrote following program & it compiles & runs fine. (see live demo here.) #include <iostream> typedef int T; int main() { int a=3; std::cout<<a<<'\n'; a.~T...

3

I have this code: struct data { void doNothing() {} }; int main() { data* ptr = new data(); ptr->~data(); ptr->doNothing(); ::operator delete(ptr); } Note that doNothing() is being c...

1

Solved

Consider the following code: #include <iostream> typedef int t; t a=42; int main() { a.t::~t(); std::cout << a; //42 } I'm expected that a will be destroyed. But it is not true, ...

1

Solved

Current Implementation I have a class containing unique_ptr fields which depend on one other: class ResourceManager { ResourceManager() {} ResourceManager(A* a_ptr) : b_ptr(new B(a)), c_ptr(...
Orderly asked 29/4, 2014 at 16:32

3

Solved

In a recent interview, I was asked to answer if this code is safe and if it is when would I use something like this: template<class T> T *CTricky<T>::Safe_Or_Not (T *object) { objec...
Hairston asked 12/2, 2014 at 5:48

1

Solved

I know that I can declare a destructor =delete or private in order to prevent the program from implicitly deleting the object at the end of scope. I also know that if it's private, I can have a mem...
Flaminius asked 17/9, 2013 at 1:49

2

Solved

Consider this code (for different values of renew and cleanse): struct T { int mem; T() { } ~T() { mem = 42; } }; // identity functions, // but breaks any connexion between input and output i...

4

Solved

Here by "simple", I mean a class with non-virtual empty destructor or POD type. Typical example: char buffer[SIZE]; T *p = new(buffer) T; ... p->~T(); // <---- always ? What happens if we...
Sthilaire asked 11/5, 2012 at 6:46
1

© 2022 - 2025 — McMap. All rights reserved.