Urthas
2nd May 2009, 21:32
Hello all,
I am new to Qt (and C++ for that matter), and as such am working through the C++ GUI Programming with Qt4 book, specifically the C++ primer appendix. I am confused regarding the discussions on implementing functions in header files, and the use of const.
To wit:
Unlike .cpp files, header files are not compilation units in their own right and...may only contain declarations that enable different compilation units to communicate with each other. Consequently, it would be inappropriate to put the square() function's implementation in a header file. - p.627
It goes on to explain that multiple (identical) implementations of the square function would arise if the header file was included more than once, which would prevent compilation. That seems clear enough.
However, later it states
...when we call a function that is declared inline...this normally leads to faster code, but might increase the size of your application. For this reason, only very short functions should be implemented inline; longer functions should always be implemented in a .cpp file. - p.632
So on the one hand I have what seems to be a hard conceptual rule. On the other hand I have a performance-oriented issue. Can someone reconcile these points of view? Would it be: "you should only implement short functions inline, and only if you know the header will not be included more than once"?
Also, regarding the use of const, I presume that this facilitates performance gains, and I'm wondering just how anal Qt programmers are in practice about using this. For example, in a discussion intended to illustrate the use of the keyword static (pp.634-35) a function is defined as:
static int instanceCount() { return counter; } // counter is a static int
I presume that it is possible to re-write that as:
static int instanceCount() const { return counter; }
but which is considered more appropriate? Why didn't the authors use const?
Thanks in advance for helping me get my feet wet. :o
I am new to Qt (and C++ for that matter), and as such am working through the C++ GUI Programming with Qt4 book, specifically the C++ primer appendix. I am confused regarding the discussions on implementing functions in header files, and the use of const.
To wit:
Unlike .cpp files, header files are not compilation units in their own right and...may only contain declarations that enable different compilation units to communicate with each other. Consequently, it would be inappropriate to put the square() function's implementation in a header file. - p.627
It goes on to explain that multiple (identical) implementations of the square function would arise if the header file was included more than once, which would prevent compilation. That seems clear enough.
However, later it states
...when we call a function that is declared inline...this normally leads to faster code, but might increase the size of your application. For this reason, only very short functions should be implemented inline; longer functions should always be implemented in a .cpp file. - p.632
So on the one hand I have what seems to be a hard conceptual rule. On the other hand I have a performance-oriented issue. Can someone reconcile these points of view? Would it be: "you should only implement short functions inline, and only if you know the header will not be included more than once"?
Also, regarding the use of const, I presume that this facilitates performance gains, and I'm wondering just how anal Qt programmers are in practice about using this. For example, in a discussion intended to illustrate the use of the keyword static (pp.634-35) a function is defined as:
static int instanceCount() { return counter; } // counter is a static int
I presume that it is possible to re-write that as:
static int instanceCount() const { return counter; }
but which is considered more appropriate? Why didn't the authors use const?
Thanks in advance for helping me get my feet wet. :o