destructor

C++11 Exception's destructor allows to throw now?

江枫思渺然 提交于 2019-11-27 07:36:24
问题 any idea why virtual ~exception() throw() is in C++98, but virtual ~exception() is in C++11? What's the design decision that allows C++11 to throw in the destructor of the class exception ? From here: c++98: class exception { public: exception () throw(); exception (const exception&) throw(); exception& operator= (const exception&) throw(); virtual ~exception() throw(); virtual const char* what() const throw(); } c++11: class exception { public: exception () noexcept; exception (const

Does explicitly calling destructor result in Undefined Behavior here?

只谈情不闲聊 提交于 2019-11-27 07:04:06
问题 In my opinion, the following code (from some C++ question) should lead to UB, but the it seems it is not. Here is the code: #include <iostream> using namespace std; class some{ public: ~some() { cout<<"some's destructor"<<endl; } }; int main() { some s; s.~some(); } and the answer is: some's destructor some's destructor I learned form c++ faq lite that we should not explicitly call destructor. I think after the explicitly call to the destructor, the object s should be deleted. The program

Why destructor is not called on exception?

风格不统一 提交于 2019-11-27 07:03:41
I expected A::~A() to be called in this program, but it isn't: #include <iostream> struct A { ~A() { std::cout << "~A()" << std::endl; } }; void f() { A a; throw "spam"; } int main() { f(); } However, if I change last line to int main() try { f(); } catch (...) { throw; } then A::~A() is called. I am compiling with "Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86" from Visual Studio 2005. Command line is cl /EHa my.cpp . Is compiler right as usual? What does standard say on this matter? lefticus The destructor is not being called because terminate() for the

Why is not deleting an object that has a destructor with a side effect undefined behavior in C++11?

蹲街弑〆低调 提交于 2019-11-27 06:48:00
问题 This answer quotes C++11 Standard 3.8: if there is no explicit call to the destructor or if a delete-expression (5.3.5) is not used to release the storage, the destructor shall not be implicitly called and any program that depends on the side effects produced by the destructor has undefined behavior. The part about the destructor not being called is clear. Now suppose the skipped destructor had a side effect that should have affected the program behavior. Why is the program behavior undefined

If changing a const object is undefined behavior then how do constructors and destructors operate with write access?

一世执手 提交于 2019-11-27 06:47:00
问题 C++ standard says that modifying an object originally declared const is undefined behavior. But then how do constructors and destructors operate? class Class { public: Class() { Change(); } ~Class() { Change(); } void Change() { data = 0; } private: int data; }; //later: const Class object; //object.Change(); - won't compile const_cast<Class&>( object ).Change();// compiles, but it's undefined behavior I mean here the constructor and destructor do exactly the same thing as the calling code,

Is destructor called if SIGINT or SIGSTP issued?

坚强是说给别人听的谎言 提交于 2019-11-27 06:44:48
I have a class with a user-defined destructor. If the class was instantiated initially, and then SIGINT is issued (using CTRL+C in unix) while the program is running, will the destructor be called? What is the behaviour for SIGSTP (CTRL + Z in unix)? No, by default, most signals cause an immediate, abnormal exit of your program. However, you can easily change the default behavior for most signals. This code shows how to make a signal exit your program normally, including calling all the usual destructors: #include <iostream> #include <signal.h> #include <unistd.h> #include <cstring> #include

MATLAB - run object destructor when using 'clear'?

拥有回忆 提交于 2019-11-27 06:44:26
问题 Suppose I have a class myClass < handle . From the Mathworks Help page on clear, Clearing handle graphics handles does not remove the objects themselves, nor does deleting the objects remove variables storing their handles. hf = figure; % Creates figure object, stores handle in variable hf delete(hf) % Removes figure object, but not the variable hf clear hf % Removes hf from the workspace; figure could still exist So clear ing a handle object does not remove it from memory unless I explicitly

Order of destruction of elements of an std::vector [duplicate]

懵懂的女人 提交于 2019-11-27 06:35:24
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: STL containers element destruction order Is there a guarantee the the elements of an std::vector would be destroyed from last to first? 回答1: 2003:5.3.5/6 says about delete[] : The delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted. In the case of an array, the elements will be destroyed in order of decreasing address (that is, in reverse order of the

Static Finalizer

[亡魂溺海] 提交于 2019-11-27 05:24:18
问题 What is the right way to perform some static finallization? There is no static destructor. The AppDomain.DomainUnload event is not raised in the default domain. The AppDomain.ProcessExit event shares the total time of the three seconds (default settings) between all event handlers, so it's not really usable. 回答1: Basically, you can't. Design your way around it to the fullest extent possible. Don't forget that a program can always terminate abruptly anyway - someone pulling out the power being

Will exit() or an exception prevent an end-of-scope destructor from being called?

二次信任 提交于 2019-11-27 05:14:57
Let's say I have the following code: struct mytype { ~mytype() { /* do something like call Mix_CloseAudio etc */ } }; int main() { mytype instant; init_stuff(); start(); return 0; } Is that destructor guaranteed to be called even if exit() is used from somewhere inside start() ? If you call exit , the destructor will not be called. From the C++ standard (§3.6.1/4): Calling the function void exit(int); declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence without destroying any objects with automatic storage duration (12.4). If exit is called to end a