Jump to content
  • Advertisement
Sign in to follow this  
tufflax

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

This topic is 4356 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!