destructor

Are moved-from objects required to be destructed?

谁都会走 提交于 2019-12-01 14:35:55
问题 If I move-construct a from b , is it still necessary to destruct b , or can I get away without doing so? This question crossed my mind during the implementation of an optional<T> template. Excerpt: ~optional() { if (initialized) { reinterpret_cast<T*>(data)->~T(); } } optional(optional&& o) : initialized(o.initialized) { if (initialized) { new(data) T(std::move(*o)); // move from o.data o.initialized = false; // o.data won't be destructed anymore! } } Of course, I could just replace the bool

destructor in Kotlin programming language

夙愿已清 提交于 2019-12-01 14:19:39
问题 I am new to Kotlin, have written a class in kotlin to perform database operation I have defined database connection in constructor using init but I want to close database connection using destructor. Any Idea of how to achieve this using kotlin destructor? Currently I have wrote a separate function to close connection, which i want to have it using destructor like any other programming language like php,etc 回答1: Handling resources that need to be closed in Kotlin You can make your Database

Internal .Net Framework Data Provider error 1

百般思念 提交于 2019-12-01 14:05:52
问题 I'm developing a WinForm app with Visual Studio 2012 Ultimate edition with all service pack, C# and .NET Framework 4.5. I get this exception: Internal .Net Framework Data Provider error 1 With this stack: en System.Data.ProviderBase.DbConnectionInternal.PrePush(Object expectedOwner) en System.Data.ProviderBase.DbConnectionPool.PutObject(DbConnectionInternal obj, Object owningObject) en System.Data.ProviderBase.DbConnectionInternal.CloseConnection(DbConnection owningObject, DbConnectionFactory

Destructor in C# basic program does not work (output missing)

我的未来我决定 提交于 2019-12-01 12:50:33
I have written the very basic program below, I am new to C#. The destructor ~Program() doesn't get called, so I do not see in the output the 'Destructor called' string. I have checked other similar questions but I don't find the answer to mine. Thanks. using System; using System.Collections.Generic; using System.Linq; using System.Text; using static System.Console; namespace LyfeCicleObject { class Program { public Program() { WriteLine("Cons called"); } ~Program() { WriteLine("Destructor called"); } static void Main(string[] args) { WriteLine("Main started"); Program p1 = new Program(); {

Destructor is called when I push_back to the vector

老子叫甜甜 提交于 2019-12-01 11:38:25
I have this class definition: class FlashStream { public: explicit FlashStream(const char * url, vector<uint8> * headers, vector<uint8> * data, void * ndata, void * notifyData = NULL, uint32 lastModified = NULL); ~FlashStream(); private: NPStream _stream; // ... } ( NPStream description ) and its implemetation: FlashStream::FlashStream(const char * url, vector<uint8> * headers, vector<uint8> * data, void * ndata, void * notifyData, uint32 lastModified) { // ... memset(&_stream, 0, sizeof(NPStream)); _stream.headers = new char[data->size()]; memcpy((void*)_stream.headers, &(*data)[0], data-

Destructor in C# basic program does not work (output missing)

醉酒当歌 提交于 2019-12-01 11:30:55
问题 I have written the very basic program below, I am new to C#. The destructor ~Program() doesn't get called, so I do not see in the output the 'Destructor called' string. I have checked other similar questions but I don't find the answer to mine. Thanks. using System; using System.Collections.Generic; using System.Linq; using System.Text; using static System.Console; namespace LyfeCicleObject { class Program { public Program() { WriteLine("Cons called"); } ~Program() { WriteLine("Destructor

An interesting case of delete and destructor (C++)

我只是一个虾纸丫 提交于 2019-12-01 11:16:06
I have a piece of code where I can call the destructor multiple times and access member functions even the destructor was called with member variables' values preserved. I was still able to access member functions after I called delete but the member variables were nullified (all to 0). And I can't double delete . Please kindly explain this. #include <iostream> using namespace std; template <typename T> void destroy(T* ptr) { ptr->~T(); } class Testing { public: Testing() : test(20) { } ~Testing() { printf("Testing is being killed!\n"); } int getTest() const { return test; } private: int test;

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

穿精又带淫゛_ 提交于 2019-12-01 09:30:46
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; newnode->next=NULL; Hashtable[index]=newnode; } else { newnode->key=key; newnode->value=value; newnode-

An interesting case of delete and destructor (C++)

帅比萌擦擦* 提交于 2019-12-01 08:57:33
问题 I have a piece of code where I can call the destructor multiple times and access member functions even the destructor was called with member variables' values preserved. I was still able to access member functions after I called delete but the member variables were nullified (all to 0). And I can't double delete . Please kindly explain this. #include <iostream> using namespace std; template <typename T> void destroy(T* ptr) { ptr->~T(); } class Testing { public: Testing() : test(20) { }

Reason reqd for the order of destructor call .?

眉间皱痕 提交于 2019-12-01 08:29:10
As I have read in certain forums,when derived class object is created base class members and methods are allocated space in the memory but there is no specific base class object. Now as the derived class object goes out of scope , why derived class destructor is called first.What is the constraint of the compiler where derived class destructor cannot be called after base class destructor..? Please correct me in case I have built a wrong understanding..Thanks in advance When a derived class object is created, there is a specific base-class object (sub-object, really). I.e., when you create a