Home Forums Programming C++ question about header files

Viewing 14 reply threads
  • Author
    Posts
    • #6013
      Anonymous
      Inactive

      Hey,

      I’ve been going back over some C++ tutorials to freshen my memory and I’ve noticed that in some header files functions are declared and implemented in the header file.

      e.g. Header files have looked like this
      [code:1:78c9bbe09f]
      class myPoint
      {
      public:

      void setX(int xCoord) { x = xCoord};
      void setY(int yCoord) { y = yCoord};


      private:
      int x;
      int y;
      };
      [/code:1:78c9bbe09f]

      I thought the point of header files was purely as an interface with the function implementation going into the .cpp file.

      Am I being too OO about it? I have spent a lot more time with Java over the last few months.

      It’s mostly been getters and setters I’ve seen in the header files, which kind of leads me to guess that maybe it’s a way to use inline implementation.

      Thanks in advance for a help, :)

      Anthony

    • #36903
      Anonymous
      Inactive

      It’s mostly been getters and setters I’ve seen in the header files, which kind of leads me to guess that maybe it’s a way to use inline implementation.[/quote:03b981fe2b]
      Yup, got it in one. The other way is to declare it with the inline keyword and then implement the function in the same header file.

    • #36905
      Anonymous
      Inactive

      Nice one!

      Thanks Satchmo :)

    • #36913
      Anonymous
      Inactive

      I may be wrong (I’ll have to check the spec), but due to occasional unexpected results* I’m pretty sure that it’ll depend on your compiler as to whether or not these methods will actually be inlined.

      * http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.3

    • #36914
      Anonymous
      Inactive

      I may be wrong (I’ll have to check the spec), but due to occasional unexpected results* I’m pretty sure that it’ll depend on your compiler as to whether or not these methods will actually be inlined.

      * http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.3%5B/quote:206e39c9dc%5D

      yep your right indeed and depending on your project settings you may even have the function call being ignored. i noticed this the other day on my code, where i was invoking a setter and the member variable was been set. So in the end i had to move the defination to a .cpp file and it got invoked every time..

    • #36916
      Anonymous
      Inactive

      Am I right in thinking that inlined functions are faster that declaring them in the cpp in certain cases? Don’t really believe it myself.
      Check out Ogre3D, have whole functions and classes completely implemented in header files, its so nasty

    • #36917
      Anonymous
      Inactive

      Am I right in thinking that inlined functions are faster that declaring them in the cpp in certain cases? Don’t really believe it myself.
      Check out Ogre3D, have whole functions and classes completely implemented in header files, its so nasty[/quote:e1bd94f4df]

      They are slightly faster, but not that much faster. There’s a bit of extra overhead that comes with function calls, like pushing parameters and return addresses onto the stack, saving / restoring registers before and after a call, and one extra jump instruction. But it depends on what you’re doing really, how much of a benefit you’ll see from this. Basically if you’re calling a function lots of times within the body of a loop, then all that extra overhead will add up and you will see some benefit from inlining.

      Just adopt the rule that if a function is only one or two statements long (such as get or set functions) then you should go inline, otherwise just declare it in the header and implement it in a .cpp file.

      I don’t know why Ogre has the arrangement you mentioned above but it’s not really the best way to be going about things. Headers as you rightly pointed out before should only act as interfaces ( except in the case of simple inline functions and template functions / classes ) and should not contain implementations. They are a form of documentation on your classes if you think about it, so you should try and keep them as tidy as possible. Also, each time you change a header file then every .cpp file including that header must be re-compiled. So it’s better to try and put your implementations into the .cpp files whenever you can, just to save on your compile times.

    • #36921
      Anonymous
      Inactive

      Thanks for all the information.

      I’m currently just doing simple tutorials so the code doesn’t have to be the most optimized, it’s good to know all the possibilities though :)

    • #36924
      Anonymous
      Inactive

      yep your right indeed and depending on your project settings you may even have the function call being ignored. i noticed this the other day on my code, where i was invoking a setter and the member variable was been set. So in the end i had to move the defination to a .cpp file and it got invoked every time..[/quote:cb4b8966d4]
      Of course inlining is only a suggestion to the compiler which it’s free to ignore and just leave it as a regular function call, but what project setting did you use to get it to ignore the function call completely?!

    • #36927
      Anonymous
      Inactive

      yep your right indeed and depending on your project settings you may even have the function call being ignored. i noticed this the other day on my code, where i was invoking a setter and the member variable was been set. So in the end i had to move the defination to a .cpp file and it got invoked every time..[/quote:d48430ad32]
      Of course inlining is only a suggestion to the compiler which it’s free to ignore and just leave it as a regular function call, but what project setting did you use to get it to ignore the function call completely?![/quote:d48430ad32]

      I honestly dont know, went up to one the project leads and was like "dude the projects fucked its stripping out my function calls on ps3". To which he replied "ah i know why that is, its a setting i must have missed when i modified the vsproj this morning". Then next sync it fixed. I’ll ask him on monday what he did to fix it, or how it started in the first place.

    • #36928
      Anonymous
      Inactive

      Ah it must have been the little-known -removeVariousImportantFunctionCalls setting, that one’s always catching me out :P

    • #36929
      Anonymous
      Inactive

      Probably more like a crappy gcc compiler setting…

    • #36931
      Anonymous
      Inactive

      Of course inlining is only a suggestion to the compiler which it’s free to ignore and just leave it as a regular function call, but what project setting did you use to get it to ignore the function call completely?![/quote:a77beafff0]

      One last point on this. If you want to ensure that a function is never inlined to guarantee a stack frame is created etc. then use this:

      [code:1:a77beafff0]#pragma auto_inline(off)

      …your function

      #pragram auto_inline(on)[/code:1:a77beafff0]

      (for Visual Studio… not so sure about gcc)

      Steve

    • #36935
      Anonymous
      Inactive

      Interesting… in what situation would you want to guarantee a stack frame? For debugging, proper callstacks etc?

    • #36937
      Anonymous
      Inactive

      Interesting… in what situation would you want to guarantee a stack frame? For debugging, proper callstacks etc?[/quote:221d59d580]

      Yeah – debugging is one case. I do remember some other case from a while back, and I forget exactly what, but something to do with wanting a copy constructor invocation due to the local variable hand-off, but I can’t remember why exactly.

      Steve

Viewing 14 reply threads
  • The forum ‘Programming’ is closed to new topics and replies.