Results 1 to 2 of 2

Thread: questions re: inline implementation, const

  1. #1
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default questions re: inline implementation, const

    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:

    Qt Code:
    1. static int instanceCount() { return counter; } // counter is a static int
    To copy to clipboard, switch view to plain text mode 

    I presume that it is possible to re-write that as:

    Qt Code:
    1. static int instanceCount() const { return counter; }
    To copy to clipboard, switch view to plain text mode 

    but which is considered more appropriate? Why didn't the authors use const?

    Thanks in advance for helping me get my feet wet.

  2. #2
    Join Date
    Apr 2009
    Posts
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: questions re: inline implementation, const

    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:
    Qt Code:
    1. #ifndef __MYHEADER_H_
    2. #def __MYHEADER_FILE_H_
    3.  
    4. //here we write header codes
    5. #endif
    To copy to clipboard, switch view to plain text mode 

    -- second, inline functions don't relate to where they are implemented! you can use them before every small function to prevent unnecessary function calls.
    Qt Code:
    1. inline int max(int a, int b){
    2. return (a<b)?b:a;
    3. }
    To copy to clipboard, switch view to plain text mode 

    --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.

Similar Threads

  1. Qy 4.4.3 MySQL driver failed
    By pamalite in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd January 2010, 01:09
  2. shared vs static
    By alisami in forum Installation and Deployment
    Replies: 3
    Last Post: 4th October 2008, 13:04
  3. qt 4.2.2 install in tru64 cxx
    By try to remember in forum Installation and Deployment
    Replies: 0
    Last Post: 30th March 2007, 07:43
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  5. Delegates and Table
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2006, 19:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.