Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. basic question to .h and .cpp files

basic question to .h and .cpp files

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++learning
5 Posts 5 Posters 0 Views 1 Watching
  • 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.
  • D Offline
    D Offline
    doneirik
    wrote on last edited by
    #1

    Hi, I´m writing on an application that is "small" but with several classes with their own header file... To avoid having to much files around I have written all the code in the header files...i.e. class declarations, function declarations and definitions etc. (I have read several places that it doesn´t really matter) This is probably bad code-writing, but is there any drawback to this? Do the compiler treat code in the .h file differenly? Is there code that cannot be written in the header file? (sorry if this is a stupid question, but still I feel like a beginner in this) doneirik

    K M T N 4 Replies Last reply
    0
    • D doneirik

      Hi, I´m writing on an application that is "small" but with several classes with their own header file... To avoid having to much files around I have written all the code in the header files...i.e. class declarations, function declarations and definitions etc. (I have read several places that it doesn´t really matter) This is probably bad code-writing, but is there any drawback to this? Do the compiler treat code in the .h file differenly? Is there code that cannot be written in the header file? (sorry if this is a stupid question, but still I feel like a beginner in this) doneirik

      K Offline
      K Offline
      KarstenK
      wrote on last edited by
      #2

      One drawback, if you change someting in one header all have to be recompiled. If you implement in cpp files this wont happen. Also "edit and continue" in debugging mode cant work properly. (?) Dont do this, stupid:doh: Try this @ home. (B&B)

      1 Reply Last reply
      0
      • D doneirik

        Hi, I´m writing on an application that is "small" but with several classes with their own header file... To avoid having to much files around I have written all the code in the header files...i.e. class declarations, function declarations and definitions etc. (I have read several places that it doesn´t really matter) This is probably bad code-writing, but is there any drawback to this? Do the compiler treat code in the .h file differenly? Is there code that cannot be written in the header file? (sorry if this is a stupid question, but still I feel like a beginner in this) doneirik

        M Offline
        M Offline
        Martijn van Kleef
        wrote on last edited by
        #3

        In general, I think everyone will agree that header files should be used for declarations, and source files for definitions. Actually, sometimes you won't be able to do without source files. For example for static variables, for initialization of global variables, etc etc

        1 Reply Last reply
        0
        • D doneirik

          Hi, I´m writing on an application that is "small" but with several classes with their own header file... To avoid having to much files around I have written all the code in the header files...i.e. class declarations, function declarations and definitions etc. (I have read several places that it doesn´t really matter) This is probably bad code-writing, but is there any drawback to this? Do the compiler treat code in the .h file differenly? Is there code that cannot be written in the header file? (sorry if this is a stupid question, but still I feel like a beginner in this) doneirik

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          doneirik wrote: To avoid having to much files... why would you like to reduce the number of files ? to have only one that is about 80Kb weight ??? :~ wow, good luck ! lol well, in my case, i prefer having several file (one header associated with a module - .cpp), where each module implements a logical section. Well, you should not be so strict either. For example, if you have a set of functions that are about to represent some Exceptions of your own in your program, you can put them in the same file, including also the functions implementation if they are writing on few lines. Of course, this is a suggestion, you can do whatever you like, but i also think that if most of experienced programmers separate the actual instructions from declarations, they do have some reasons... doneirik wrote: Do the compiler treat code in the .h file differenly? actually, .h files are not compiled. The #include directive tells the pre-processor to copy the content of the .h file where such instruction is written in a file. doneirik wrote: Is there code that cannot be written in the header file? Yep, affirmative !! A header can contain : - named namespaces (namespace N { /*...*/ }); - Types definitions (struct Point { int x, y; };) - Templates declarations (template<class T> class Z;) - Templates definitions (template<class T> class V { /*...*/ };) - Functions declarations (extern int strlen(const char*);) - inline functions definitions (inline char get(char* p) { return *p++; }) - Datas declarations (extern int a;) - Constants definitions (const float pi = 3.141593;) - Enumerations (enum Light { red, yellow, green };) - Names declarations (class Matrix) - Inclusion directives (#include <algorithm>) - Macros definitions (#define VERSION 12) - Conditional compilation directives (#ifdef _cplusplus) - Comments (/* End of file */) In the other hand, a header might never contain : - Ordinary functions definitions (char get(char* p) { return *p++; }) - Datas definitions (int a) - Arrays definitions (short tbl[] = {1, 2, 3};) - Non-named namespaces (namespace { /*...*/ }) - Exported templates definitions (expor

          1 Reply Last reply
          0
          • D doneirik

            Hi, I´m writing on an application that is "small" but with several classes with their own header file... To avoid having to much files around I have written all the code in the header files...i.e. class declarations, function declarations and definitions etc. (I have read several places that it doesn´t really matter) This is probably bad code-writing, but is there any drawback to this? Do the compiler treat code in the .h file differenly? Is there code that cannot be written in the header file? (sorry if this is a stupid question, but still I feel like a beginner in this) doneirik

            N Offline
            N Offline
            Nitron
            wrote on last edited by
            #5

            Just reorg your directories and put arrange the files that way. The only implementation I put in header files is 1-line returns and/or single "set" functions. At least I'm not as bad as some people I know, they have a seperate cpp file for every function in the header! My rule is, break out another cpp if your function is 30-ish lines or longer, just for clarity. (although there should be very few cases where you need such a big function) ~Nitron.


            ññòòïðïðB A
            start

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups