User Tools

Site Tools


qt:heap_versus_stack

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
qt:heap_versus_stack [2010/12/08 22:12] – [Short answer] mithatqt:heap_versus_stack [2010/12/08 22:22] – [Heap or Stack?] mithat
Line 1: Line 1:
 ====== Heap or Stack? ====== ====== Heap or Stack? ======
  
-Should Qt objects be created on the heap or on the stack?((Reminder: the heap is the memory store from which dynamically allocated storage is taken; the stack is used for standard variable storage as well as for passing values to functions, etc.))+Should Qt objects be created on the heap or on the stack?((Reminder: the heap is the memory store from which dynamically allocated storage is taken; the stack is used for standard variable storage as well as for passing actual parameters to functions, etc.))
  
 ===== Short answer ===== ===== Short answer =====
Line 14: Line 14:
  
 For this to work, there are two requirements: For this to work, there are two requirements:
-  - All child objects must be created on the heap; however, the root itself may be on the stack. In other words, child objects that you intend to place in the tree must be created dynamically (using ''new'').+  - All child objects must be created on the heap; however, the root itself may be on the stack. In other words, child objects that you intend to place in the tree must be created dynamically using the ''new'' operator.
   - Child objects must explicitly be made children of their parents (i.e., they must be //reparented//). Constructors for (re)parentable Qt objects let you pass parameters to specify the parent.   - Child objects must explicitly be made children of their parents (i.e., they must be //reparented//). Constructors for (re)parentable Qt objects let you pass parameters to specify the parent.
  
Line 20: Line 20:
 <code cpp-qt> <code cpp-qt>
 QObjectSubclass theParent;                              // create theParent on the stack QObjectSubclass theParent;                              // create theParent on the stack
- +AnotherQObjectSubclass *foo;                            // pointer to a child 
-AnotherQObjectSubclass *foo; +foo = new AnotherQObjectSubclass( "bar", &theParent );  // create foo on the heap and reparent to theParent
-foo = new AnotherQObjectSubclass( "foo", &theParent );  // create foo on the heap and reparent to theParent+
 </code> </code>
  
qt/heap_versus_stack.txt · Last modified: 2010/12/08 22:59 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki