Function bodies in headers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • inmatarian

    #1

    Function bodies in headers

    I sometimes see, particularly in wxWidgets, member function bodies
    inside of header files. They're never all that long, and it's usually
    just constructors and occasional accessors. Question is, doesn't that
    create copies in all of the source files that include that header?
  • Gianni Mariani

    #2
    Re: Function bodies in headers

    inmatarian wrote:
    I sometimes see, particularly in wxWidgets, member function bodies
    inside of header files. They're never all that long, and it's usually
    just constructors and occasional accessors. Question is, doesn't that
    create copies in all of the source files that include that header?
    If you see "inline" functions in header files then there will likely be
    a copy in each object file. The linker is supposed to remove dupes in
    this case. You must make sure to recompile everything that uses a
    header if the function in the header file changes to avoid surprises.

    Comment

    • EventHelix.com

      #3
      Re: Function bodies in headers

      On Oct 22, 5:46 pm, inmatarian <inmatar...@gma il.comwrote:
      I sometimes see, particularly in wxWidgets, member function bodies
      inside of header files. They're never all that long, and it's usually
      just constructors and occasional accessors. Question is, doesn't that
      create copies in all of the source files that include that header?
      If the function is more than a few lines, the header file functions
      will be mapped to regular functions.
      This should not result in any code bloat. Only one copy of the
      function is present.

      Smaller functions will be treated as inline.

      --
      VisualEther - http://www.eventhelix.com/VisualEther/
      Reverse engineer sequence diagrams from Wireshark logs

      Comment

      • inmatarian

        #4
        Re: Function bodies in headers

        EventHelix.com wrote:
        On Oct 22, 5:46 pm, inmatarian <inmatar...@gma il.comwrote:
        >I sometimes see, particularly in wxWidgets, member function bodies
        >inside of header files. They're never all that long, and it's usually
        >just constructors and occasional accessors. Question is, doesn't that
        >create copies in all of the source files that include that header?
        >
        If the function is more than a few lines, the header file functions
        will be mapped to regular functions.
        This should not result in any code bloat. Only one copy of the
        function is present.
        >
        Smaller functions will be treated as inline.
        Oh, I'm sure that a good optomizing compiler would inline the function.
        What I'm asking is what would happen with all optimization turned off?
        Does the function show up in each compiled object file?

        Comment

        • gallows

          #5
          Re: Function bodies in headers

          On 23 Ott, 06:58, inmatarian <inmatar...@gma il.comwrote:
          Oh, I'm sure that a good optomizing compiler would inline the function.
          What I'm asking is what would happen with all optimization turned off?
          Does the function show up in each compiled object file?
          Uhm, you should get errors like "multiple definition of foo()".
          Strange approach, can you show us the code?

          Comment

          • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

            #6
            Re: Function bodies in headers

            On 2007-10-23 06:58, inmatarian wrote:
            EventHelix.com wrote:
            >On Oct 22, 5:46 pm, inmatarian <inmatar...@gma il.comwrote:
            >>I sometimes see, particularly in wxWidgets, member function bodies
            >>inside of header files. They're never all that long, and it's usually
            >>just constructors and occasional accessors. Question is, doesn't that
            >>create copies in all of the source files that include that header?
            >>
            >If the function is more than a few lines, the header file functions
            >will be mapped to regular functions.
            >This should not result in any code bloat. Only one copy of the
            >function is present.
            >>
            >Smaller functions will be treated as inline.
            >
            Oh, I'm sure that a good optomizing compiler would inline the function.
            What I'm asking is what would happen with all optimization turned off?
            Does the function show up in each compiled object file?
            Assuming your compiler/linker does not remove any "dead code" when
            optimisations are off then you should have one copy in each object file
            built from code that included your header. When you then use the linker
            to combine those into an executable file only one of those will be kept.

            --
            Erik Wikström

            Comment

            Working...