[Qt-interest] close app before exec()
Constantin Makshin
dinosaur-rus at users.sourceforge.net
Thu Jul 2 17:38:14 CEST 2009
Use initializers to set all class members to the "uninitialized" state
(NULL for pointers, etc.).
Constructors don't have a return value, so the only ways to tell about an
error in the constructor are:
1) "everything is OK" pointer/reference parameter like the one suggested
earlier in this thread;
2) exceptions.
Any error in the constructor makes the object semi-constructed, but (1)
also makes the code of both object creation and the constructor itself
ugly (IMHO).
There's a nice exceptions FAQ at http://parashift.com/c++-faq-lite,
particularly its error and exceptions handling part at
http://www.parashift.com/c++-faq-lite/exceptions.html . I think it may be
useful for all of us (no offence). :)
On Thu, 02 Jul 2009 13:51:22 +0400, RFOG <no at mail.com> wrote:
> And you get a object semi constructed. What memory is assinged and what
> unassigned? Call destructor or not call? Etc.
>
> I think no.
>
> On Wed, 01 Jul 2009 21:38:01 +0200, Constantin Makshin
> <dinosaur-rus at users.sourceforge.net> wrote:
>
>> Actually, exceptions are the best way to tell about an error in the
>> constructor. :)
>>
>> On Wed, 01 Jul 2009 13:08:20 +0400, RFOG <no at mail.com> wrote:
>>> Or better than that: throw an exception in ctor and catch in main...
>>>
>>> I know is not a good idea fire an exception in a ctor, but if you are
>>> going to exit...
>>>
>>> On Wed, 01 Jul 2009 11:04:40 +0200, RFOG <no at mail.com> wrote:
>>>
>>>> Modfiy the ctor to receive a boolean by reference and the do something
>>>> like this:
>>>>
>>>> bool bExit;
>>>> MainWindow w(bExit); <--- Here I check for conditions...
>>>> if(!bExit)
>>>> {
>>>> w.show();
>>>> return a.exec();
>>>> }
>>>>
>>>> MainWindows ctor signature coud be:
>>>>
>>>> MainWindow(bool& bexit)
>>>> {
>>>> ...
>>>> }
>>>>
>>>> I do not know if it will work with QT, but seems a common scenario.
>>>> However you cannot bye sayng nothing to the user...
>>>>
>>>> On Wed, 01 Jul 2009 10:57:31 +0200, OS Prog <osprog at gmail.com> wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I have QMainWindow application. In the QMainWindow constructor I
>>>>> check
>>>>> for
>>>>> conditions and if they are not met I want to exit from the
>>>>> application.
>>>>> The
>>>>> close(), QApplication::exit(), and so on doesn't work, because they
>>>>> are
>>>>> before application.exec().
>>>>>
>>>>> In main.cpp I have
>>>>> ...
>>>>> MainWindow w; <--- Here I check for conditions...
>>>>>
>>>>> w.show();
>>>>>
>>>>> return a.exec();
>>>>>
>>>>> ...
>>>>>
>>>>> Regards,
>>>>> Nikolay
--
Constantin "Dinosaur" Makshin
More information about the Qt-interest-old
mailing list