Results 1 to 7 of 7

Thread: Why use * ?

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Why use * ?

    What are the benefits of uwing * ? Example:

    QTextBox * myTextBox = new QTextBox("hello");

    in stead of:

    QtextBox myTextBox("hello");

    Which one is better, why ?

  2. #2
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

  3. #3
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Why use * ?

    You might use RAII ... since it safely releases resources. Not sure what you were asking. From what I've read Qt helps out greatly.

  4. #4
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why use * ?

    Well... the question is for curiosity.

    I would like to know when and why I should use the "*" to create a new instance of a class in stead of instantiating without it.

    class test
    {
    .
    .
    }

    int main()
    {
    test * myTest = new test();
    // or perhaps I should just use:
    test myTest();
    }

  5. #5
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Why use * ?

    I would default to non-pointer types unless design dictates it ... employing RAII for safety in releasing resources. If you need pointer types, I'd recommend using smart pointers (i.e. reference counting pointers). But, the question is so general I'm not sure I'm answering correctly.

    Here's what I've traditionally used for smart pointers. It's from the Boost library. I'll never understand why a smart pointer wasn't included in the C++ Standard. Qt may provide a smart pointer ... don't know yet.
    http://www.boost.org/libs/smart_ptr/shared_ptr.htm

  6. #6
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Why use * ?

    The Qt Tutorial address memory management issues ... concerning Qt widgets.

    From the Qt Tutorial -- Chapter 4

    "Note that quit is a local variable in the constructor. MyWidget does not keep track of it; Qt does, and will automatically delete it when the MyWidget object is deleted. This is why MyWidget doesn't need a destructor. (On the other hand, there is no harm in deleting a child when you choose to. The child will automatically tell Qt about its imminent death.)"

    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. ...
    5. QPushButton *quit = new QPushButton(tr("Quit"), this);
    6. quit->setGeometry(62, 40, 75, 30);
    7. quit->setFont(QFont("Times", 18, QFont::Bold));
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Mar 2006
    Posts
    22
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Why use * ?

    Simple, use pointers when you need the object to stay in memory. Regular objects are created on the stack, and thus destroyed quite quickly.

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.