destructor

Can gdb break on implicit class methods?

风流意气都作罢 提交于 2019-12-18 20:54:32
问题 The compiler generates some class methods like copy constructors, destructors, etc. Is it possible to have gdb break on those methods to, e.g., observe where objects are being copied or destroyed? 回答1: Can gdb break on implicit class methods? Yes, of course, it can. (gdb) break MyClass::MyClass(const MyClass &) // break when copied (gdb) break MyClass::~MyClass() // break when object destroyed as simple as that. These are breakpoints based, NOT on file:line, but on function names. If you've a

Why can I not call my class's constructor from an instance of that class in C++?

本小妞迷上赌 提交于 2019-12-18 18:28:51
问题 When can an object of a class call the destructor of that class, as if it's a regular function? Why can't it call the constructor of the same class, as one of its regular functions? Why does the compiler stops us from doing this? For example: class c { public: void add() ; c(); ~c() ; }; void main() { c objC ; objC.add() ; objC.~c() ; // this line compiles objC.c() ; // compilation error } 回答1: By definition, a constructor is only called once, when the object is created. If you have access to

destructors in Qt4

泪湿孤枕 提交于 2019-12-18 13:07:54
问题 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

A proper way of destroying a TThread object

五迷三道 提交于 2019-12-18 12:55:28
问题 This question may seem trivial, but I hope you won't ignore it. Before destroying a TThread object it is usually necessary to wait until the thread that called the TThread.Execute() method finishes, for only then can we be sure that, for instance, the objects destroyed inside the class's destructor are no longer accessed. Therefore it is necessary to call Terminate to set the Terminated flag that the thread has to check to know whether to exit or not, and then call the WaitFor() method.

What is the point of deleted destructor?

五迷三道 提交于 2019-12-18 06:57:32
问题 I come across the rule (section N3797::12.8/11 [class.copy] ) An implicitly-declared copy/move constructor is an inline public member of its class. A defaulted copy/ move constructor for a class X is defined as deleted (8.4.3) if X has: [...] — any direct or virtual base class or non-static data member of a type with a destructor that is deleted or inaccessible from the defaulted constructor, or [...] But I can't get the point of deleted destructor appearing in a virtual or direct base class

How to write destructor for union-like class

▼魔方 西西 提交于 2019-12-18 05:56:37
问题 I'm trying to use an union (C++) that has some non-primitive variables, but I'm stuck trying to create the destructor for that class. As I have read, it is not possible to guess what variable of the union is being used so there is no implicit destructor, and as I'm using this union on the stack, the compiler errors that the destructor is deleted. The union is as follows: struct LuaVariant { LuaVariant() : type(VARIANT_NONE) { } LuaVariantType_t type; union { std::string text; Position pos;

Aren't destructors guaranteed to finish running?

a 夏天 提交于 2019-12-18 04:53:01
问题 Destructors are weird . I was attempting to eliminate the need of using the disposable pattern by using 'smart' reference management, ensuring that the garbage collector could collect objects at the correct time. In one of my destructors I had to wait for an event from another object, which I noticed it didn't. The application simply shut down and the destructor was terminated in middle of execution. I'd expect a destructor is always allowed to finish running, but as the following test

PHP: destructor vs register_shutdown_function

时光毁灭记忆、已成空白 提交于 2019-12-18 03:24:09
问题 I have a PHP class that creates a PNG image on the fly and sends it to browser. PHP manual says that I need to make sure that imagedestroy function is called at end to release the memory. Now, if I weren't using a class, I would have some code like this: function shutdown_func() { global $img; if ($img) imagedestroy($img); } register_shutdown_function("shutdown_func"); However, I believe that appropriate place for my class would be to place a call to imagedestroy in class' destructor. I

How to write a simple class in C++?

天大地大妈咪最大 提交于 2019-12-17 22:35:52
问题 I have been reading a lot of tutorials on C++ class but they miss something that other tutorials include. Can someone please show me how to write and use a very simple C++ class that uses visibility, methods and a simple constructor and destructor? 回答1: Well documented example taken and explained better from Constructors and Destructors in C++: #include <iostream> // for cout and cin class Cat // begin declaration of the class { public: // begin public section Cat(int initialAge); //

How to make a threaded network server in Qt?

怎甘沉沦 提交于 2019-12-17 22:28:33
问题 I'm working on a threaded telnet server (one thread per connection), and can't figure out how to get rid of valgrind errors. I've narrowed the problem down to WHERE I delete the tcpsocket. I create the QTcpSocket in the run() method of the QThread: void TelnetConnection::run() { tcpSocketPtr = new QTcpSocket(); if (!tcpSocketPtr->setSocketDescriptor(socketDescriptor)) { emit error(tcpSocketPtr->error()); return; } .... } When my app wants to disconnect the client I call: void TelnetConnection