Sign in to follow this  
tufflax

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

Recommended Posts

Hey! I'm learning C++ and I read that member functions of a class that is defined within the class definition are inline automatically, and I can't help but wonder why. As opposed to functions that are only decalared within the class definition and defined somewhere else. 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

Share this post


Link to post
Share on other sites
Well, i just started learning about classes, yesterday, but Ive learned about inline functions awhile back. I remember reading that the compiler has the ultimate decision to whether or not to make the funtion loined or not, depending on if the compiler thinks it benfitial.

As to yur second question, no clue buddy [smile]

Hope i helped a little bit.

Share this post


Link to post
Share on other sites
Yeah but, what I meant was that defining the member functions inside the class definition is like typing the inline keyword in front of it.

Share this post


Link to post
Share on other sites
Inline functions are, as Nuclear Rabbit said, ultimately decided by the compiler. 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), but the latter should really be avoided unless you know what you are doing.

edit: Remember, simply using the "inline" keyword in a function definition does not garuntee that it will be inlined.
edit2: jpetrie knows considerably more about the C++ standard than I do.

Quote:

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've never really heard that before, but I suppose it means that a class's member functions are not defined in memory untill you instantiate an instance of that class.

[Edited by - Driv3MeFar on August 18, 2006 11:12:47 PM]

Share this post


Link to post
Share on other sites
Quote:

I'm learning C++ and I read that member functions of a class that is defined within the class definition are inline automatically, and I can't help but wonder why.


Because that's the way it has to be. See below.


Quote:

As for all member function being inlined automatically, I don't know where you read that but it sounds a bit fishy to me.


It is correct, more or less. Member functions whose definition appears in the class defintion are inline functions, i.e., the "inline" specifier is implicit in their declaration, with all the caveats that entails; the function still may not get inlined if the compiler decides not to. This behavior is dictated by the standard.

The standard also says that there can be at most one definition of a non-inline function in a program, but there may be multiple definitions of inline functions. From this, you can infer why functions defined in the body of a class must be implicitly qualified with the inline keyword: if they weren't, it would be possible for multiple definitions of that function to exist if the header were included in multiple translation units.

Share this post


Link to post
Share on other sites
Yeah, that's what I though too, about the second question.

Thank you both for the answers.

I've been googling some more for the inline member functions, and it seems to indeed be the case. Although I've not seen an explaination for it yet.

Share this post


Link to post
Share on other sites
Furthermore,

Quote:

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


Quote:

I've never really heard that before, but I suppose it means that a class's member functions are not defined in memory untill you instantiate an instance of that class.


The original source of tufflax's quote does not appear to be particularly reputable. Member functions are no different that regular functions -- no more than one definition (except for inline functions, as mentioned before) exists (this leads to what is true: the function is not included in the storage of a class instance, i.e., you can add as many member functions as you want to a class and you will not change the result of the sizeof() operator applied to an instance of that class). Additionally, the machine code generated from that functions definition is part of the compiled executable and, consequently, always exists. It is mapped into memory just like the rest of the executable when the OS loads it.

If you think about it, it isn't possible for the code to magically come into being once the first object of a given class type is instantiated. The code would have to come from somewhere.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
If you think about it, it isn't possible for the code to magically come into being once the first object of a given class type is instantiated. The code would have to come from somewhere.


I didn't think that sounded correct, thanks for clarifying.

Share this post


Link to post
Share on other sites
jpetrie, thanks for your insight. I just have one more question. What happends if two inline functions with the same signature/declarator is encountered, but with different bodies? Compile error?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this