Results 1 to 6 of 6

Thread: Why should I use pointers ?

Hybrid View

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

    Default Re: Why should I use pointers ?

    Very good explanation, now:

    I have looked at some books about Qt, the ones I know of show always something like:

    QTextBrowser *textBrowser;

    Why ?

    In the ".h" class couldn't I just do:

    QTextBrowser textBrowser;

    ????

  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: Why should I use pointers ?

    You could but this has two disadvantages:
    1. You have to include all header files needed by the class in every file that uses your class
    2. The order of destroying objects depends on the compiler. If it destroys objects in a wrong order, you'll get a memory error, because an area of memory will be freed twice. Consider this example:

    Qt Code:
    1. class A : public QWidget {
    2. public:
    3. A() : QWidget(){
    4. w1.setParent(&w2);
    5. }
    6. private:
    7. QWidget w1;
    8. QWidget w2;
    9. };
    To copy to clipboard, switch view to plain text mode 
    If compiler first frees w1 and then w2 then everything will be ok - w1 will be destroyed, so it will detach from its parent and then w2 will be destroyed.
    If the order is w2 and then w1, then when w2 is destroyed, it will destroy all its children including w1, so w1 will be destroyed as well. But the compiler doesn't know that w1 has already been destroyed, so will proceed normally and will try to delete w1, which will result in a double-free and a memory error.

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

    Default Re: Why should I use pointers ?

    So... what is the best approach:

    QTextBrowser textBrowser;

    or

    QTextBrowser *textBrowser

    ???

  4. #4
    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: Why should I use pointers ?

    It depends on the situation. It is safe to use pointers in every situation, but sometimes it is not required to do so (for example for widgets that don't have a parent or that will be destroyed earlier than their parents). There is really no "better". Qt handles most memory management for you, so you even don't have to remember about releasing the memory yourself, hence using "new" is not a big effort and can save your butt when writing for platforms that have a limited stack space

Similar Threads

  1. Is it possible to store pointers in a database ?
    By probine in forum General Programming
    Replies: 8
    Last Post: 5th April 2006, 21:28
  2. another Q_OBJECT and pointers (?)
    By caminoix in forum Qt Programming
    Replies: 8
    Last Post: 26th March 2006, 21:06
  3. Pointers and Q_PROPERTY
    By andersin in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2006, 14:05
  4. Problem with pointers while using localtime() and time()
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 11th January 2006, 15:48
  5. K&R's strcpy function - problem with pointers
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 8th January 2006, 15:16

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.