Function bodies in headers

I

inmatarian

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?
 
G

Gianni Mariani

inmatarian said:
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.
 
E

EventHelix.com

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.
 
I

inmatarian

EventHelix.com said:
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?
 
G

gallows

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?
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top