destructor

How to write a correct Hash Table destructor in c++

人盡茶涼 提交于 2019-12-30 11:25:06
问题 I am writing a c++ Hashtable Here is my destructor: HashMap::~HashMap() { for (int i=0; i<cap; i++) { Node* ptr = Hashtable[i]; while (ptr!=NULL) { Node* delptr; delptr=ptr; ptr=ptr->next; delete delptr; } } delete [] Hashtable; } My add function: void HashMap::add(const std::string& key, const std::string& value) { int index = hashfunction(key)%cap;; Node* ptr=Hashtable[index]; Node* newnode=new Node; if (contains(key)==false) { if (ptr == nullptr) { newnode->key=key; newnode->value=value;

Explicit destructor in templated context

早过忘川 提交于 2019-12-30 08:13:42
问题 I want to explicitly destroy a vector in a templated context. The following works for me (GNU C++ 4.3, 4.4 and Clang++ 1.1): template <typename T> void destroy_vector_owner(VectorOwner<T> *obj) { obj->v.~vector(); // further cleanup by Python API functions omitted } while it fails on Mac OS X v10.5's g++ ( i686-apple-darwin10-gcc-4.2.1 ) with expected class-name before ‘(’ token If I change it to obj->v.~vector<T>(); the code fails to compile with G++, but Clang can still handle it. Which is

Explicit destructor in templated context

丶灬走出姿态 提交于 2019-12-30 08:13:24
问题 I want to explicitly destroy a vector in a templated context. The following works for me (GNU C++ 4.3, 4.4 and Clang++ 1.1): template <typename T> void destroy_vector_owner(VectorOwner<T> *obj) { obj->v.~vector(); // further cleanup by Python API functions omitted } while it fails on Mac OS X v10.5's g++ ( i686-apple-darwin10-gcc-4.2.1 ) with expected class-name before ‘(’ token If I change it to obj->v.~vector<T>(); the code fails to compile with G++, but Clang can still handle it. Which is

std::enable_shared_from_this: is it allowed to call shared_from_this() in destructor?

可紊 提交于 2019-12-30 08:08:48
问题 #include <memory> #include <iostream> struct A : public std::enable_shared_from_this<A> { ~A() { auto this_ptr = shared_from_this(); // std::bad_weak_ptr exception here. std::cout << "this: " << this_ptr; } }; int main() { auto a = std::make_shared<A>(); a.reset(); return 0; } I'm getting std::bad_weak_ptr exception when calling shared_from_this() . Is it by design? Yes, it might be dangerous as this pointer can't be used after the destructor returns, but I don't see a reason why it would be

Deleting derived classes in std::unique_ptr<Base> containers

青春壹個敷衍的年華 提交于 2019-12-30 07:31:35
问题 I'm a little confused. Basically, I've got 2 different resource managers (AudioLibrary and VideoLibrary) that both inherit from a shared BaseLibrary class. This base class contains references to both audio and video. Both audio and video inherit from a parent class called Media. I'm keeping the data in a map, filled with unique_ptr. But, to my surprise, I've discovered when these pointers are eventually deleted via .erase, only the base destructor for Media is called. I guess I've missed

What is the order of destruction of objects in VBScript?

两盒软妹~` 提交于 2019-12-30 05:00:06
问题 In what order are objects in a .vbs destroyed? That is, given these globals: Set x = New Xxx Set y = New Yyy I'm interested in answers to any of the following. For instances of classes implemented in the .VBS, in what order will Class_Terminate be called? Cursory poking suggests in the order ( not reverse order!) of creation, but is this guaranteed? EDIT : I understand that Class_Terminate will be called when the last last reference to an object is released. What I meant was: in what order

Cannot access private member in singleton class destructor

孤街浪徒 提交于 2019-12-29 08:52:08
问题 I'm trying to implement this singleton class. But I encountered this error: 'Singleton::~Singleton': cannot access private member declared in class 'Singleton' This is flagged in the header file, the last line which contains the closing brace. Can somebody help me explain what is causing this problem? Below is my source code. Singleton.h: class Singleton { public: static Singleton* Instance() { if( !pInstance ) { if( destroyed ) { // throw exception } else { Create(); } } return pInstance; }

Cannot access private member in singleton class destructor

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-29 08:52:05
问题 I'm trying to implement this singleton class. But I encountered this error: 'Singleton::~Singleton': cannot access private member declared in class 'Singleton' This is flagged in the header file, the last line which contains the closing brace. Can somebody help me explain what is causing this problem? Below is my source code. Singleton.h: class Singleton { public: static Singleton* Instance() { if( !pInstance ) { if( destroyed ) { // throw exception } else { Create(); } } return pInstance; }

Usage of “this” in destructor

扶醉桌前 提交于 2019-12-28 21:49:14
问题 Is it valid to call some function in destructor with this argument? Function does not store pointer, but assume full-functional object. 回答1: this is still valid in the destructor. However, you need bear in mind that virtual functions no longer work properly as you might expect once the object is being destroyed; see e.g. Never Call Virtual Functions during Construction or Destruction. Essentially, the dynamic type of the object is modified as each destructor completes. 回答2: In one word: YES.

Will (global) static variables be destroyed at program end? [duplicate]

北城以北 提交于 2019-12-28 19:16:47
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Does C++ call destructors for global and class static variables? What is the lifetime of global MyClass myclass; global static MyClass myclass; global const MyClass myclass; global static const MyClass myclass; function local static MyClass myclass; when its initialization actually occured global static constexpr MyClass myclass; in C++11 and especially will they be destroyed on regular program end (i.e. main is