PDA

View Full Version : questions re: inline implementation, const



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

zaghaghi
2nd May 2009, 23:41
Hi,

you should read C++ more carefully, those things that you mixed them with each other are different things. I hope that i can clear some concepts for you.
-- first, using header files and cpp file:

as you should know, we can write all program codes in a single file and then compile it, but it's not a good idea, but we can write them in separate files according to their coherence.

in a common solution, we can write declaration of each class in a .h file and its implementation in a . cpp file and every where we need each class #include its header file.

with the help of #def, #ifndef macros we can prevent multiple inclusion of header files.
e.g. for myheader.h file:


#ifndef __MYHEADER_H_
#def __MYHEADER_FILE_H_

//here we write header codes
#endif


-- second, inline functions don't relate to where they are implemented! you can use them before every small function to prevent unnecessary function calls.


inline int max(int a, int b){
return (a<b)?b:a;
}

--third, static member functions, are functions that can be used with class name, and need not an instance of an object of that class. and const member functions are usefull when you have a const instance of an object. so it's not necessary that define const static functions.