C++, Why are member funcs defined in class def. inline?

Started by
11 comments, last by tufflax 17 years, 8 months ago
Quote:Original post by deathkrush
Quote:Original post by tufflax

Also, I read "The (member) functions are not definitions in the sense that memory is set aside for the function code; this doesn't happen until an object of the class is created." I'm not exactly sure what the writer meant by this. Can someone help me out here? :P


I think the write meant that if you don't call the member function anywhere in your code it won't be compiled as part of your class. It's true with non-member function as well.


Yeah, something like that was what I thought when I read it. If it isn't even mentioned in the source code, the compiler might leave it out. But what if (I'm not really good at this...) the code is part of a, say, dll file or something that has been compiled separatly? (I'm not really sure what a dll is, guess I'll be learning that soon.)
Advertisement
Quote:
I think the write meant that if you don't call the member function anywhere in your code it won't be compiled as part of your class. It's true with non-member function as well.


Not quite: it will still be compiled. If the compiler were the one discarding unreferenced functions, you'd basically end up with a zero-size (or very nearly so) executable! The compiler knows nothing about any translation units except the one it is compiling. Consequently, it can't make the determination. Just because a function defined in translation unit Foo is never called from translation unit Foo doesn't mean nobody will ever call it (for example, from translation unit Bar), and since the compiler has no visibility on any other translation unit as it is compiling translation unit Foo, it cannot omit the function.

The linker, however, can discard unreferenced functions.

Quote:
But what if (I'm not really good at this...) the code is part of a, say, dll file or something that has been compiled separatly? (I'm not really sure what a dll is, guess I'll be learning that soon.)


A .dll is an .exe that's treated slightly differently (they use the same Portable Executable format). The PE format, DLL's and EXE's are concepts of the Windows operating system, not C++, however.

Typically, a particularly toolchain will have a way to mark functions as "exported" from a DLL or its equivalent on other platforms. Those functions will be considered "referenced" by the linker, if the linker is told to discard unreferenced functions. This is, once again, not a standard C++ concept.


Quote:
What happends if two inline functions with the same signature/declarator is encountered, but with different bodies? Compile error?


The inline function must be defined in every translation unit, and have the same definition in each. The definition of the inline function has to be visible to all translation units that use it (i.e., it has to be in the header). If you don't make its definition visible, you'll get a linker error (so you can't bury the definition in two different .cpp files and try to give it different bodies that way). If you try to define it twice within a header / within a translation unit, you'll get a compiler error.

And since I'm being particularly pedantic this evening:

Quote:
You can use the inline keyword to suggest that a function should be inlined, and there is also a pre-processor constant to force a function to be inlined (__forceinline)


__forceinline is a vendor-specific keyword, not a pre-processor constant. As you mentioned, it should be avoided unless you know what you're doing (especially since, despite the name, it can still fail to inline a function).

[Edited by - jpetrie on August 19, 2006 12:03:36 AM]
Thanks again for the very good answers!



[Edited by - tufflax on August 19, 2006 11:22:23 AM]

This topic is closed to new replies.

Advertisement