destructor

something like stackbased objects in c++ for javascript

◇◆丶佛笑我妖孽 提交于 2019-12-07 05:53:59
问题 Looking for a construct in javascript which works like the destructor in stackbased or local object in c++, e.g. #include <stdio.h> class M { public: int cnt; M() {cnt=0;} void inc() {cnt++;} ~M() {printf ("Count is %d\n", cnt);} }; ... {M m; ... m.inc (); ... m.inc (); } // here the destructor of m will printf "Count is 2"); so this means I am looking for a construct which does an action when its scope is ending (when it "goes out of scope"). It should be robust in the way that it does not

What does empty destructor do?

喜你入骨 提交于 2019-12-07 05:52:11
问题 I heard that empty destructor doesn't do anything and calling it doesn't remove the object. But in the code: #include <iostream> #include <set> class a { public: ~a() {} std::set <int> myset; }; int main() { a object; object.myset.insert(55); object.~a(); object.myset.insert(20); std::cout << object.myset.size(); } I get: " * glibc detected * /.app: double free or corruption (fasttop):" and then "ABORT". If it matters I have c++11 flag enabled. So what does empty constructor actually do? It

Destructors and inheritance in C++?

人盡茶涼 提交于 2019-12-07 05:43:56
问题 I use Borland C++ Builder. And i had o problem #include <Classes.hpp> class TMyObject : public TObject { __fastcall TMyObject(); __fastcall ~TMyObject();//I would like to inherite my destructor from TObject }; __fastcall TMyObject::TMyObject() : TObject()//it will inherited my constructor from TObject { } And for that new destructor that will inherite ~TObject ? __fastcall TMyObject::~TMyObject????????????? 回答1: This can be solved at TObject 's level. Its destructor has to be virtual:

C++ abstract class destructor [closed]

时间秒杀一切 提交于 2019-12-07 05:36:04
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . Is it good practice (and is it possible) to create an abstract class using only a pure virtual destructor in the parent class? Here's a sample class AbstractBase { public: AbstractBase () {} virtual ~AbstractBase () = 0; }; class Derived : public AbstractBase { public: Derived() {} virtual ~Derived

C++: Explicitly call destructor of template parameter's typedef

ぐ巨炮叔叔 提交于 2019-12-07 04:10:00
问题 I have the following: template <typename X> struct A { typedef X _X; }; template <typename Y> struct B { // Y is struct A typename Y::_X x; void call_destructor () { x.~Y::_X(); // This doesn't work x.Y::~_X(); // This as well } }; which doesn't compile, saying that qualified type does not match destructor name Using the keyword typename before the call also does not work. However, the following does compile: template <typename Y> struct B { typename Y::_X x; typedef typename Y::_X __X; void

Why do I always get “terminate called after throwing an instance of…” when throwing in my destructor?

偶尔善良 提交于 2019-12-07 03:46:11
问题 I'm trying to write a unit test that detects an invalid use of the lock() feature of my class. In order to do so, I want to use the destructor and throw an exception from there. Unfortunately, instead of catching the exception, g++ decides to call std::terminate(). There is a very simplified version of the class: class A { public: A() : f_lock(0) {} ~A() { if(f_lock) throw my_exception("still locked"); } lock() { ++f_lock; } unlock() { --f_lock; } private: int f_lock; }; There is a valid test

Non virtual destructor in base class, but virtual destructor in derived class cause segmentation fault

不想你离开。 提交于 2019-12-07 01:32:27
问题 recently in a job interview I was asked about the problem of leaking memory in derived classes when the base class's destructor is not declared virtual. I wrote a small test to confirm my answer, but I found something interesting. Obviously, if you create a Derived object via new but store its pointer as a Base* , the derived object's destructor won't be called, if the pointer is deleted (so much for my answer to the question). I thought whether the derived class's destructor is virtual or

Destructor vs member function race

流过昼夜 提交于 2019-12-07 01:16:44
问题 When I'm inside a destructor is it possible that some other thread will start executing object's member function? How to deal with this situation? 回答1: C++ has no intrinsic protection against using an object after it's been deleting - forget about race conditions - another thread could use your object after it's been completely deleted. Either: Make sure only one place in the code owns the object, and it's responsible for deleting when no-one is using the object. Make the object reference

Class Destructor Problem

时光总嘲笑我的痴心妄想 提交于 2019-12-07 00:23:17
问题 I am making a simple class that contains a StreamWrite class Logger { private StreamWriter sw; private DateTime LastTime; public Logger(string filename) { LastTime = DateTime.Now; sw = new StreamWriter(filename); } public void Write(string s) { sw.WriteLine((DateTime.Now-LastTime).Ticks/10000+":"+ s); LastTime = DateTime.Now; } public void Flush() { sw.Flush(); } ~Logger() { sw.Close();//Raises Exception! } } But when I close this StreamWriter in the destructor, it raises an exception that

When is destructor called in a WCF service

点点圈 提交于 2019-12-06 21:19:56
问题 I need to create a service which will maintain a WCF session. In the constructor I read in data from the DB and when the session ends I have to save it back. If I understand correctly the session ends when I call Close() on the Client (My client ServiceClient was created with SvcUtil.exe). When I test it I see that it is sometimes called after approx. 10 minutes, sometimes after 20 minutes and sometimes not at all. So when is the destructor called? Service [ServiceBehavior(InstanceContextMode