View Full Version : Declare buttons in .h file or in .cpp file?

18th March 2006, 10:42
In the Qt examples (t14) I find that they declare buttons and other things in the constructor of the GameBoard class. Is there any special reason for not putting it in the .h file instead?
The only reason I can think of is that you don't have to write #include <QPushButton> twice. Any better reason?

18th March 2006, 11:11
They don't care about using those widgets after initialization so there's no need wasting memory keeping track of them with pointers.

Pointers are 32bits or 64bits, depending on your CPU. Thus there are 4 or 8 bytes wasted by widget and by instance of the class :

Let's imagine you've got an app that create 100 widgets inside a QTabWidget, each of these widget holding 100 children => 100*100*4 = 40kb wasted (or 80 on a 64bit CPU). Then you're using a multi threaded OS aren't you. Asssuming you're runnig 20 apps of the same kind => 20*40 = 800kb (or 1.6 Mb on a 64bit CPU) . Numbers I chose may not seem realistic but IMHO they're an underevaluation of what some beginners do...