acctualy base class will do that for you.
of course this is the case only with Qt classes, that's why you dont have to delete label in that case.
QApplication class will delete it via its destructor.
The object would be reference-counted, not the pointer. If what I said earlier is true, the QtApplication would reference the object, which would notify it when it gets deleted so it can take it off the list of objects.
@ne555
what I dont like are 'Q' letters for every damn class name, that's only thing why I hate Qt.
it would be better (maybe) if all class names etc would be wrapped into "Qt" namespace like std and boost classes are.
however from another side I like Qt only cos it's made on native C++ and easy to learn.
and most important is very well documented. (at least that's my opinion)
From C++ GUI Programming with Qt 4 Second Edition by Jasmin Blanchette and Mark Summerfield (The book Trolltech uses to teach Qt to its own new hires.)
"For simplicity, we don't bother calling delete on the QLabel object at the end of the main() function. This memory leak is harmless in such a small program, since the memory will be reclaimed by the operating system when the program terminates."
From what I gathered, that last statement is bollocks. When main ends, app is destroyed and thus, it's destructor is called, therefor all items get deallocated and the day is safed. That is, in this example. When you decided to not return app.exec(), but make it a seperate statement, I would personally put delete statements between said statement and the return 0 statement. Then again, in the given example, the label needn't be dynamically allocated.
In given example object label dosn't require dynamic alocaion but in some real project that object(s) may be very big and you'll probably consider about alocation.
@darkestfright: there is no point. I just don't like the syntax.
¿why do I need to tell the type of the variable twice? Besides, there are objects in c++
A new without delete looks weird (especially considering that is assigned to a raw pointer)
But new and delete in the same scope, looks ugly.
From what I gathered, that last statement is bollocks. When main ends, app is destroyed and thus, it's destructor is called, therefor all items get deallocated and the day is safed.
I'm 99.9% sure that in C++, destructors are not automatically called for dynamically allocated data like in the OP and Alrededor's post.
Kyon wrote:
Then again, in the given example, the label needn't be dynamically allocated.
This. Don't ever dynamically allocate data when you don't need to. Thanks Kyon for pointing this out ;)