Do I have to delete it? [Qt]

后端 未结 3 1503
臣服心动
臣服心动 2021-01-12 16:57

Do I have to delete objects from the heap in the example below? And if yes, how?

#include 
#include 
#include 

        
3条回答
  •  抹茶落季
    2021-01-12 17:23

    Instead of managing the memory manually, you can let the compiler do it for you. At that point you may ask: why use the heap at all? You should keep things by value as much as feasible, and let the compiler do the hard work.

    The objects will be destroyed in the reverse order of declaration. Thus the splitter - the implicit parent - must be declared first, so that it doesn't attempt to incorrectly delete its children. In C++, the order of declarations has a meaning!

    int main(int argc, char* argv[])
    {
        QApplication app(argc,argv);
        QSplitter splitter;
        QTreeView tree;
        QListView list;
        QTableView table;
        splitter.addWidget(&tree);
        splitter.addWidget(&list);
        splitter.addWidget(&table);
        splitter.show();
        return app.exec();
    }
    

提交回复
热议问题