include .h or .cpp file?

This is a discussion on include .h or .cpp file? within the C++ Programming forums, part of the General Programming Boards category; If a person has created a class they have 2 files: the .h file (header file?) and the .cpp file ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    115

    include .h or .cpp file?

    If a person has created a class they have 2 files: the .h file (header file?) and the .cpp file (implementation file?)
    Now to use the class in a main file, should the the .h file be included (which in turn includes the implementation file) or vice-versa? Different people I ask give me different answers.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Never include a .cpp file.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The only possible sticky point is if you have template code, since that code needs to be visible. OTOH, there are lots of extensions that aren't ".cpp" you can use for such code, so really the rule is still: Never include a .cpp file. AND ALSO: The statement that the .h file include the implementation file is also greatly mistaken.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    115
    Quote Originally Posted by tabstop View Post
    The only possible sticky point is if you have template code, since that code needs to be visible. OTOH, there are lots of extensions that aren't ".cpp" you can use for such code, so really the rule is still: Never include a .cpp file. AND ALSO: The statement that the .h file include the implementation file is also greatly mistaken.
    pardon?

    So always include the header file unless it is templated, in which case include the implementation file or use an alternative solution? Why do you say that the header file including the implementation file is a misconception? is it because it should be including the object code of the implementation file or am I getting confused?

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Implementation files include headers. In the case of templates you have to break the rule if you want your source and header in sep. files but even then they usually have an .hpp, .inl, or some other extension. I prefer .hpp.

    Header files do not include implementation except for the aforementioned template exception.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. C programing doubt
    By sivasankari in forum C Programming
    Replies: 2
    Last Post: 04-29-2008, 09:19 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 12:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21