Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Class implementation in header file

Class implementation in header file

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qatto
    wrote on 16 Aug 2013, 15:26 last edited by
    #1

    Hello,

    Am writing an app with several classes and the structure is pretty hard to get. So i find myself having to put one of the class implementations together with the declaration in the header file so as to remove the cpp file for a less complex #include structure. But is this practice healthy for the application?
    Thanks in advance

    Web/Desktop Developer

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on 16 Aug 2013, 16:07 last edited by
      #2

      Common practice is: Have each class' implementation in a separate source (.cpp) file and have the class' declaration in a separate header (h.) file. Thus, for a class CMyClass you'd have "MyClass.cpp" plus "MyClass.h".

      You should think of the class' header file as its "public" interface, i.e. that is what other must neccessarily include to use the class - but not more! Therefore you should keep the header file as clean as possible to avoid dependency nightmare! You should avoid any #include directives in a header file, unless absolutely necessary. Most of the time, you can move the #include into the source (.cpp) file and use a forward declaration in the header file.

      Finally, I'd avoid putting two or more class declarations into the same header (.h) file, unless these two classes are always used together anyway.

      --
      @/* ---- MyClass.h ---- */

      //Forward declration
      class COtherClass;

      //Class declaration
      class CMyClass
      {
      public:
      CMyClass(void);
      ~CMyClass(void);
      private:
      COtherClass *m_data;
      };

      /* ---- MyClass.cpp ---- */

      #include "MyClass.h"
      #include "OtherClass.h"

      CMyClass::CMyClass(void)
      {
      m_data = new OtherClass();
      }

      CMyClass::~CMyClass(void)
      {
      delete m_data;
      }

      [...]@

      --

      BTW: Only cases, where you must have implementation directly in the header file, is (a) template classes and (b) inline functions. In all other cases, IMO, the implementation should never be in the header file!

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0

      1/2

      16 Aug 2013, 15:26

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved