destructor

Transitioning to C++11 where destructors are implicitly declared with noexcept

六眼飞鱼酱① 提交于 2019-12-04 17:42:57
问题 In C++11, a destructor without any exception specification is implicitly declared with noexcept , which is a change from C++03. Therefore, a code which used to throw from destructors in C++03 would still compile fine in C++11, but will crash at runtime once it attempts throwing from such a destructor. Since there's no compile-time error with such a code, how could it be safely transitioned to C++11, short of declaring all and every existing destructor in the code base as being noexcept(false)

Inheritance and Destructors in C#

孤人 提交于 2019-12-04 17:17:26
问题 According to this, it states that Destructors cannot be inherited or overloaded. In my case, for all subclasses, the destructors will be identical. Is this pretty much telling me that I must define the same destructor in each sub class. There is no way that I can declare the destructor in the base class and have the handle the destruction? Say I have something like this: class A { ~A() { SomethingA(); } } class B : A { } B b = new B(); When B is destroyed, its destructor wont be called? 回答1:

Should destructors be threadsafe?

匆匆过客 提交于 2019-12-04 16:34:37
问题 I was going through a legacy code and found the following snippet: MyClass::~MyClass() { EnterCriticalSection(&cs); //Access Data Members, **NO Global** members are being accessed here LeaveCriticalSection(&cs); } I am wondering will it help by any chance to guard the destructor ? Consider a scenario : 1. Thread1 - About to execute any of the member function which uses critical section 2. Thread2- About to execute destructor. If the order of execution is 1=>2 then it might work. But what if

Java, executing a method when object's scope ends

泪湿孤枕 提交于 2019-12-04 16:26:29
问题 I've an object with a certain state. The object is passed around and it's state is temporarly altered. Something like: public void doSomething(MyObject obj) { obj.saveState(); obj.changeState(...); obj.use(); obj.loadState(); } In C++ it's possible to use the scope of an object to run some code when constructing and distructing, like NeatManager(MyObject obj) { obj.saveState(); } ~NeatManager() { obj.loadState(); } and call it like void doSomething(MyObject obj) { NeatManager mng(obj); obj

Can `weakref` callbacks replace `__del__`?

落花浮王杯 提交于 2019-12-04 16:11:37
问题 Is there any obstacle that prevents weakref from doing everything that __del__ does but with much stronger guarantees (e.g., finalize guarantees that the call will be made before the interpreter exits, and the order of calls is well-defined, etc.)? It seems that in the distant past it was thought that weakref would eventually lead to the removal of __del__ from the language. What prevented this from happening? There seems to be few use cases for __del__, and all the ones I'm aware of seem to

Can you guarantee destructor order when objects are declared on a stack?

前提是你 提交于 2019-12-04 15:50:00
问题 I have code that controls a mutex lock/unlock based on scope: void PerformLogin() { ScopeLock < Lock > LoginLock( &m_LoginLock ); doLoginCommand(); ScopeLock < SharedMemoryBase > MemoryLock( &m_SharedMemory ); doStoreLogin(); ... } Can I guarantee that MemoryLock will be destructed before LoginLock ? 回答1: Yes, it is. In any particular scope local objects are destroyed in the reverse order that they were constructed. 回答2: Yes, destructors are called in the reverse order of construction. 回答3:

Excel VBA object constructor and destructor

家住魔仙堡 提交于 2019-12-04 15:20:33
问题 I need to make some custom objects in VBA that will need to reference each other and I have a some issues. First - how do object constructors work in VBA? Are there constructors? Second - are there destructors? How does VBA handle the end of the object lifecycle? If I have an object that references others (and this is their only reference), then can I set it to Nothing and be done with it or could that produce memory leaks? This quasi-OO stuff is just a little bit irritating. 回答1: VBA

Catching exceptions in destructors

馋奶兔 提交于 2019-12-04 15:19:24
问题 Is it possible to make a destructor catch exceptions and then re-throw them? If so, how would I do that, since there isn't a clear place for a try statement? Basically, I want to ideally do: CMyObject::~CMyObject() { catch(...) // Catch without a try. Possible? { LogSomeInfo(); throw; // re-throw the same exception } // Normal Destructor operations } Background I have a large, complex application that is throwing an unhandled exception somewhere. I don't have easy access to main or the top

Delete expression

自闭症网瘾萝莉.ら 提交于 2019-12-04 12:25:53
Reference here That destructor will also implicitly call the destructor of the auto_ptr object. And that will delete the pointer it holds, that points to the C object - without knowing the definition of C! That appeared in the .cpp file where struct A's constructor is defined. This was curious and then 5.3.5/5 states - "If the object being deleted has incomplete class type at the point of deletion and the complete class has a non-trivial destructor or a deallocation function, the behavior is undefined." My question is that why isn't such a program which attempts to delete a pointer to an

How can I handle interrupt signal and call destructor in c++? [duplicate]

喜夏-厌秋 提交于 2019-12-04 12:01:32
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is destructor called if SIGINT or SIGSTP issued? My code like this: #include <iostream> #include <signal.h> #include <cstdlib> void handler(int) { std::cout << "will exit..." << std::endl; exit(0); } class A { public: A() {std::cout << "constructor" << std::endl;} ~A() {std::cout << "destructor" << std::endl;} }; int main(void) { signal(SIGINT, &handler); A a; for (;;); return 0; } When I pressed Ctrl-C, it