Re: Using inline

From: Jack Klein (jackklein_at_spamcop.net)
Date: 04/18/04


Date: Sun, 18 Apr 2004 13:04:56 -0500

On Sun, 18 Apr 2004 11:09:00 -0500, "Ryan Stewart"
<zzanNOtozz@gSPAMo.com> wrote in alt.comp.lang.learn.c-c++:

> "Jeff Schwab" <jeffplus@comcast.net> wrote in message
> news:nPCdnS-f96gmFB_dRVn-iQ@comcast.com...
> > Ryan Stewart wrote:
> > > "Jeff Schwab" <jeffplus@comcast.net> wrote in message
> > > news:dpmdnfjp7Z-c6x_d4p2dnA@comcast.com...
> > >
> > >>Tony Johansson wrote:
> > >>
> > >>>Hello Experts!
> > >>>
> > >>>My question is if I have a member function called func for class x and
> I
> > >>>want this member function func to be compiled inline where can I put
> the
> > >>>definition of this member function.
> > >>>
> > >>>Is it in the definition file of class x which is called x.h
> > >>>Is in the implementation file for class x called x.cpp.
> > >>>
> > >>>
> > >>>I 'm readaing in a book and here it says that functions that is to
> > >
> > > defined
> > >
> > >>>inline must be put in the definition file for class x but I think they
> > >
> > > might
> > >
> > >>>be wrong.
> > >>>
> > >>>So that's why I ask you out there what you think?
> > >>
> > >>The book is right.
> > >
> > >
> > > Half right, I would say. You can define the function within the class
> > > definition *or* you can define it externally like normal and add the
> > > "inline" keyword. At least that's how I've understood it.
> >
> > The question wasn't whether the function definition needs to go in the
> > class definition, but whether it needs to go in the same file.
> >
> > The definition just needs to be given before the function is used in
> > each translation unit. For an inline member function, this effectively
> > means that yes, the function definition goes in the same file as the
> > class definition. If the function is only going to be used in one TU,
> > why is it a member of a class defined in an external header?
>
> Maybe I'm misinterpreting the original question. I'm new to C++, and the
> inexact terminology is giving me fits. To my understanding, the OP was
> asking where the definition (ie the implementation/body, not the prototype)
> of a member function went if it was to be inline. The answer to that, also
> in my understanding, is that the function body can go in the class
> definition (declaration?) itself (in the header file) or it can go in the
> implementation file (the cpp file) with the "inline" specifier.

You are misunderstanding how inline functions work in C++, unless you
are planning on #includ'ing the implementation file along with the
header file in all source files that use the class.

In order for the compiler to generate code for a function inline, it
must have the definition of the function in scope. If the header file
merely has the inline keyword on a member function definition, and
does not contain the body of the function, then the compiler cannot
insert the function inline in a source file that just includes the
header. It doesn't have access to the function and does not know what
code to insert.

For member functions that are not inline, you are quite correct.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html


Relevant Pages

  • Re: inline functions
    ... > I want to ask about the real usefulness of the 'inline' keyword. ... > expand a function not declared as 'inline' by the programmer. ... The compiler cannot inline a function that is in a different ... > corresponding header file, with the consequences (recompilation of all ...
    (comp.lang.cpp)
  • Re: Function delcared inline but not defined inline
    ... > I declare a function as inline in the header file ... > What would the compiler do in this case? ... because it isn't "visible" to gcc when it is compiling main.c ...
    (comp.lang.c)
  • Re: Using inline
    ... of a member function went if it was to be inline. ... in my understanding, is that the function body can go in the class ... implementation file with the "inline" specifier. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: LNK2005
    ... If you move the member function definition outside the class definition then ... >>you have to use the inline keyword there also. ... small I don't worry about code bloat ...
    (microsoft.public.vc.language)
  • Re: inline class member functions
    ... > Rules for inline functions say that they have to be defined in the same ... > the class or within the same header file. ... > address of the inline member functions, the compiler would be forced to ...
    (comp.lang.cpp)