Re: Using inline
From: Jack Klein (jackklein_at_spamcop.net)
Date: 04/18/04
- Next message: Chris K: "aggregation: allocating memory to member pointers after constructor?"
- Previous message: James Dennett: "Re: Newbie static class member question [C++]"
- In reply to: Ryan Stewart: "Re: Using inline"
- Next in thread: Ryan Stewart: "Re: Using inline"
- Reply: Ryan Stewart: "Re: Using inline"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Chris K: "aggregation: allocating memory to member pointers after constructor?"
- Previous message: James Dennett: "Re: Newbie static class member question [C++]"
- In reply to: Ryan Stewart: "Re: Using inline"
- Next in thread: Ryan Stewart: "Re: Using inline"
- Reply: Ryan Stewart: "Re: Using inline"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|