destructor

Goto out of a block: do destructors get called?

旧巷老猫 提交于 2019-11-30 13:01:39
问题 Consider the following code: void foo() { { CSomeClass bar; // Some code here... goto label; // and here... } label: // and here... } Will the destructor of bar be called ? 回答1: The C++ Standard says: On exit from a scope (however accomplished), destructors (12.4) are called for all constructed objects with automatic storage duration (3.7.2) (named objects or temporaries) that are declared in that scope, in the reverse order of their declaration. So the answer is "yes". 回答2: Yes, they will be

Why is vector deleting destructor being called as a result of a scalar delete?

╄→尐↘猪︶ㄣ 提交于 2019-11-30 12:14:57
I have some code that is crashing in a large system. However, the code essentially boils down to the following pseudo-code. I've removed much of the detail, as I have tried to boil this down to the bare bones; I don't think this misses anything crucial though. // in a DLL: #ifdef _DLL #define DLLEXP __declspec(dllexport) #else #define DLLEXP __declspec(dllimport) #endif class DLLEXP MyClass // base class; virtual { public: MyClass() {}; virtual ~MyClass() {}; some_method () = 0; // pure virtual // no member data }; class DLLEXP MyClassImp : public MyClass { public: MyClassImp( some_parameters

When I kill a pThread in C++, do destructors of objects on stacks get called?

别说谁变了你拦得住时间么 提交于 2019-11-30 12:14:00
I'm writing a multi-threaded C++ program. I plan on killing threads. However, I am also using a ref-counted GC. I'm wondering if stack allocated objects get destructed when a thread gets killed. The stack does not unwind when you 'kill' a thread. Killing threads is not a robust way to operate - resources they have open, such as files, remain open until the process closes. Furthermore, if they hold open any locks at the time you close them, the lock likely remains locked. Remember, you are likely calling a lot of platform code you do not control and you can't always see these things. The

What is a non-trivial destructor in C++?

我的未来我决定 提交于 2019-11-30 11:42:33
I was reading this which mentions destructors being trivial and non-trivial. A class has a non-trivial destructor if it either has an explicitly defined destructor, or if it has a member object or a base class that has a non-trivial destructor. In example, I have a class, class C { public: ~C(); // not explicitly declared. }; If C::~C() is implicitly defined does it make a trival dtor? Kerrek SB You are getting your words mixed up. Your example does indeed declare an explicit destructor. You just forget to define it, too, so you'll get a linker error. The rule is very straight-forward: Does

Why is the destructor not called for the returned object from the function?

百般思念 提交于 2019-11-30 11:11:16
I was thinking that when a function returns an object on the stack to the calling function, the calling function gets a copy of the original object but the original object's destructor is called as soon as the stack unwinds. But in the following program, the destructor is getting called only once. I expected it to be called twice. #include <iostream> class MyClass { public: ~MyClass() { std::cout << "destructor of MyClass" << std::endl; } }; MyClass getMyClass() { MyClass obj = MyClass(); return obj; // dtor call for obj here? } int main() { MyClass myobj = getMyClass(); return 0; // Another

Why base class destructor (virtual) is called when a derived class object is deleted?

只谈情不闲聊 提交于 2019-11-30 10:57:11
问题 A difference between a destructor (of course also the constructor) and other member functions is that, if a regular member function has a body at the derived class, only the version at Derived class gets executed. Whereas in case of destructors, both derived as well as base class versions get executed? It will be great to know what exactly happens in case of destructor (maybe virtual) & constructor, that they are called for all its base classes even if the most derived class object is deleted

How can I call a private destructor from a shared_ptr?

醉酒当歌 提交于 2019-11-30 09:30:01
I have a resource_manager class which maintains a std::vector<boost::shared_ptr<resource> > internally. resource_manager is a friend class of resource . I want resource s to only be created/deleted by resource_manager , so I made its constructors private (which works ok). However, if I make the destructor private, the code doesn't compile because the destructor is called by boost::shared_ptr , which is not a friend of resource . I am thinking of enforcing the "do not delete by clients" rule by only returning only const resource* from the resource_manager , but somehow I am not satisfied with

php destructor behaviour

霸气de小男生 提交于 2019-11-30 09:04:18
im trying to understand php constructor and destructor behaviour. Everything goes as expected with the constructor but i am having trouble getting the destructor to fire implicitly. Ive done all the reading on php.net and related sites, but i cant find an answer to this question. If i have a simple class, something like: class test{ public function __construct(){ print "contructing<br>"; } public function __destruct(){ print "destroying<br>"; } } and i call it with something like: $t = new test; it prints the constructor message. However, i'd expect that when the scripts ends and the page is

destructors in Qt4

倾然丶 夕夏残阳落幕 提交于 2019-11-30 08:54:30
I'm very confused about using destructors in Qt4 and hope, you guys can help me. When I have a method like this (with "Des" is a class): void Widget::create() { Des *test = new Des; test->show(); } how can I make sure that this widget is going to be deleted after it was closed? And in class "Des" i have this: Des::Des() { QPushButton *push = new QPushButton("neu"); QHBoxLayout *layout = new QHBoxLayout; layout->addWidget(push); setLayout(layout); } where and how do I have to delete *push and *layout? what should be in the destructor Des::~Des() ? Another option to using deleteLater() , or

Are STL containers designed to allow inheritance? [duplicate]

旧街凉风 提交于 2019-11-30 08:45:04
Possible Duplicate: Is it okay to inherit implementation from STL containers, rather than delegate? My question is related to Why don't STL containers have virtual destructors? Some people (including author of the former question) are convinced that not having virtual dtor implies that class is not inheritable. I am skeptical about such a strong statement, so I asked for the source or some reasoning but most respondents remain silent. Also nobody responded to my answer So I think it is good idea to question the assumptions made in the former question and clarify this important issue. Are STL