Langleson
Langleson

Reputation: 103

Store member variables on heap or stack when extending QMainWindow?

I have a program (written in c++ using Qt) which shows an image in a window and takes measurements and draws to the image. All of the Qt tutorials use the heap to store user interface objects so I have done the same.

When I extend the QMainWindow class, I add a number of member variables which I use to store measurements and different versions of the image. I do not explicitly store these on the heap (no new identifier), but my understanding is if the class is defined on the heap, then so are its members.

Questions:

If the class instance is defined with new

  1. Should the UI elements be pointers and defined with 'new' or should they just be variables and used right away? (Perhaps because UI objects can be created with parents, this makes cleanup easy.)

  2. If they should be assigned with new in the above question, should all member variables (measurements and images) also be used with new and then deleted?

Hopefully my vocabulary is correct.

Upvotes: 0

Views: 93

Answers (1)

LogicStuff
LogicStuff

Reputation: 19617

It does not matter whether UI objects will be allocated dynamically, or reside on the stack (yes, if the whole object is allocated on the heap, so are its members, but again, it does not matter whether they're pointers or not - on the other hand, performance might be worse with pointers - additional space, allocation).

In both cases, you don't have to care about resource management. That however, is not the case with your own types not deriving from QObject and not having the window as a parent. You want to avoid raw or smart pointers, where you don't need them.

Upvotes: 2

Related Questions