Results 1 to 2 of 2

Thread: constructors, new, return

  1. #1
    Join Date
    Jan 2006
    Munich, Germany.
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4

    Default constructors, new, return

    Hello all,
    I think a widget constructor in Qt will return a zero pointer if construction can't be done. Is that right? Well, how is that done?
    Qt Code:
    1. AThing* myThing = new AThing(someParameters);
    2. if(myThing)//check to see if made correctly
    3. ...
    To copy to clipboard, switch view to plain text mode 
    When I try to return anything from my constructors I get a compiler error - can't return a value. new() is doing the work here and is returning the pointer, no? Can I get my constructor to get a zero pointer (or any other value) returned from new()?

    In the above example AThing() needs a port, or a file, or some memory, which it can't get. After the construction, I can check with some func e.g.
    bool myThing->constructedProperly() but maybe my object failed to allocate enough memory for itself so that I can't even call that func.


  2. #2
    Join Date
    Jan 2006
    Warsaw, Poland
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits

    Default Re: constructors, new, return

    It is "new" which returns a pointer to an object or 0 if it can't allocate memory for it, not the constructor. The latter only creates the object in the part of memory it is given. It is called by the compiler for both heap allocated (through operator "new") and stack allocated objects. You can't return values from the constructor, you can only throw exceptions or use some flags inside the object itself, which can then be checked from outside (like Qt does with isNull() or isValid() methods).

Similar Threads

  1. [SOLVED] DirectShow Video Player Inside Qt
    By ToddAtWSU in forum Qt Programming
    Replies: 16
    Last Post: 3rd November 2011, 07:47
  2. Carriage Return in QString
    By incapacitant in forum Newbie
    Replies: 7
    Last Post: 2nd December 2010, 09:18
  3. When is the best time to delete a QCanvasItem
    By irudkin in forum Qt Programming
    Replies: 12
    Last Post: 8th March 2007, 21:28
  4. Version 4.1.3 and QTreeView
    By greencastor in forum Qt Programming
    Replies: 2
    Last Post: 13th June 2006, 10:37
  5. Replies: 4
    Last Post: 24th March 2006, 22:50


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.