Results 1 to 3 of 3

Thread: When should I use new operator?

  1. #1
    Join Date
    Sep 2009
    Posts
    20
    Thanked 2 Times in 2 Posts

    Default When should I use new operator?

    Hello all!

    I'm have some expirience in pure C++ programming
    and I have basic knowlegde about dynamic memory allocation

    For example if I want to create table wih X int elemtens I use

    int *pint = new int[X]; // later - delete [] pint;

    but in Qt there are a lot of dynamic objects, for example shoud I use new
    operator for QList class

    QList *myList = new QList; or
    QString * myString = new QString;

    or just

    QList myList;
    QString myString;

    so my question is - when should I use new operator in Qt?

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

    Default Re: When should I use new operator?

    A general rule is to use the new operator with all objects that are derived from QObject with optional exclusion of those that you do not pass a parent (like often QSettings or QFile) or those that are shortlived (like QDialog that will have QDialog::exec() called on it). The second general rule is to allocate all objects that can be copied (ones that have a public copy constructor, such as QList, QColor, QFontMetrics, etc.) on the stack (without the new operator). Most objects in Qt can be copied, so most can be created on the stack.
    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. The following user says thank you to wysota for this useful post:

    ravas (2nd July 2015)

  4. #3
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: When should I use new operator?

    You may want to read up on Pointers.
    http://www.tek-tips.com/faqs.cfm?fid=2914
    Using pointers in Qt is no different to using them in pure C++.

    On a side note QList is a template Class so you need to specify what You want it to contain. For example the declaration for a list of doubles would look something like this:
    Qt Code:
    1. QList<double> myList;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. new operator
    By QTInfinity in forum Qt Programming
    Replies: 1
    Last Post: 22nd December 2008, 11:56
  2. std::max_element and overload operator()
    By mickey in forum General Programming
    Replies: 2
    Last Post: 8th September 2008, 18:28
  3. operator [] overloading
    By darksaga in forum General Programming
    Replies: 5
    Last Post: 8th April 2008, 15:27
  4. The -> Operator
    By keifer in forum Newbie
    Replies: 1
    Last Post: 11th March 2008, 02:24
  5. QString == Operator
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 27th December 2006, 17:08

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
  •  
Qt is a trademark of The Qt Company.