Results 1 to 5 of 5

Thread: Question about initializing an object without assigning...

  1. #1
    Join Date
    Mar 2011
    Posts
    29
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Question about initializing an object without assigning...

    Qt Code:
    1. int main(...) {
    2. .....
    3. Application app(argc, argv);
    4. new ApplicationUI(&app);
    5. .....
    6. }
    To copy to clipboard, switch view to plain text mode 

    I have seen a code like the above a few times but I don't understand why is it valid. Where does the initalized object go?

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

    Default Re: Question about initializing an object without assigning...

    It doesn't "go" anywhere, it is on a heap like any other object allocated with "new", it is just not assigned to any public variable.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2011
    Posts
    29
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about initializing an object without assigning...

    And what would be a good use case for initializing an object that I cannot access through I variable?
    I'd like to fill this hole in my C++ knowledge but I cannot figure it out yet.

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

    Default Re: Question about initializing an object without assigning...

    C++ has nothing to do with this. You don't need to assign the return value of new to be able to use the object. For instance the object might "install" itself on some other object in its constructor or you might simply not care to access the object directly in your app.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Question about initializing an object without assigning...

    Quote Originally Posted by wysota View Post
    C++ has nothing to do with this. You don't need to assign the return value of new to be able to use the object. For instance the object might "install" itself on some other object in its constructor or you might simply not care to access the object directly in your app.
    wysota is technically correct. However, both of these uses would generally be considered bad style and potentially dangerous. If you see a memory allocation like that, it is most likely a memory leak. Again, though, in this specific example, the ApplicationUI probably 'imprints' itself into the given Application object.

Similar Threads

  1. QHash initializing
    By trust88 in forum Qt Programming
    Replies: 6
    Last Post: 6th May 2013, 22:31
  2. initializing QPainter with a QImage
    By Aayush in forum Qt Programming
    Replies: 3
    Last Post: 20th October 2011, 12:47
  3. Loading/Initializing classes
    By alexandernst in forum Qt Programming
    Replies: 6
    Last Post: 6th December 2010, 12:06
  4. Delay initializing?
    By MorrisLiang in forum Newbie
    Replies: 4
    Last Post: 23rd August 2010, 15:37
  5. question about assigning a pixmap to an icon
    By Dark_Tower in forum Newbie
    Replies: 5
    Last Post: 21st December 2009, 09:11

Bookmarks

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.