CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: Inline Members

  1. #1
    Join Date
    Sep 2008
    Posts
    113

    Inline Members

    Sorry for such a simple question. I just want to clarify something.

    If a method is defined inside the class declaration, it's automatically considered inline, correct? This would mean that I should avoid doing so except for very simple methods or constructors.

    I'm pretty confident about this, but I'm looking at a large amount of code from a program here where all of the class methods are defined inside the class declaration and it didn't make sense to me. The best choice if you don't want to separate the implementation from the declaration would be to define the methods inside the header file, but outside the class declaration, right?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Inline Members

    Quote Originally Posted by Raislin
    If a method is defined inside the class declaration, it's automatically considered inline, correct?
    Yes, though more precisely you mean class definition rather than class declarations in general.

    Quote Originally Posted by Raislin
    This would mean that I should avoid doing so except for very simple methods or constructors.
    Note that the compiler has the option of not inlining despite your suggestion that it do so.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Sep 2008
    Posts
    113

    Re: Inline Members

    What's the difference between declaration and definition as far as classes go? I'm not sure I'm getting that.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Inline Members

    Quote Originally Posted by Raislin
    What's the difference between declaration and definition as far as classes go?
    A definition is a declaration, but a declaration can also refer to a forward declaration such as:
    Code:
    class X;
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Inline Members

    Look at example for function declaration and function definition.
    Thanks for your help.

  6. #6
    Join Date
    Sep 2008
    Posts
    113

    Re: Inline Members

    That makes sense.

  7. #7
    Join Date
    May 2009
    Posts
    28

    Re: Inline Members

    Quote Originally Posted by laserlight View Post
    A definition is a declaration, but a declaration can also refer to a forward declaration such as:
    Code:
    class X;
    I always thought that the stuff in header file (class name, its members function etc) is a declaration whereas the definitions are the details of those declarations that go in .cpp file. Is it incorrect to think of it this way?

    sgiri

  8. #8
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Inline Members

    Quote Originally Posted by sgiri1981 View Post
    I always thought that the stuff in header file (class name, its members function etc) is a declaration whereas the definitions are the details of those declarations that go in .cpp file. Is it incorrect to think of it this way?
    For functions, yes. For structs and classes, no.

    EDIT:
    Well, that probably deserves some elaboration.
    In general, a declaration tells the compiler that something exists. You can then refer to what you have declared and the compiler accepts it, because you've told it that it exists.
    A definition actually creates something (be it a variable, function, class, ...). It is the task of the linker to relate whatever you referred to in your code, to whatever you created.

    E.g.
    Code:
    #include <iostream>
    
    extern int n; // declaration of a variable
    
    class X; // (forward) declaration of a class
    
    void foo(X x); // function declaration
    
    class X
    {
    public:
    	void bar()
    	{
    		std::cout << "X::bar()" << std::endl;
    		++n;
    	}
    }; // class definition
    
    int n = 0; // definition of variable
    
    void foo(X x)
    {
    	x.bar();
    } // function definition
    
    int main()
    {
    	X x;
    	foo(x);
    }
    As you can see, I can use X and n before they are defined (but they are declared).
    However, I cannot define foo() before I have defined X, because it uses a member function of X. If X is only declared, but not defined, the compiler doesn't know X::bar exists.
    Last edited by D_Drmmr; June 10th, 2009 at 09:22 AM.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  9. #9
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Inline Members

    Quote Originally Posted by sgiri1981 View Post
    I always thought that the stuff in header file (class name, its members function etc) is a declaration whereas the definitions are the details of those declarations that go in .cpp file. Is it incorrect to think of it this way?

    sgiri
    This is how I think of it:
    Code:
    // Declaration.
    class Foo;
    
    // Declaration.
    class Foo
    {
    public:
        void Func();
    };
    
    // Definition.
    void Foo::Func()
    {
    }
    
    // Declaration and definition.
    class Foo
    {
    public:
        void Func()
        {
        }
    };
    Last edited by Mybowlcut; June 10th, 2009 at 09:18 AM. Reason: Wrong comment...
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  10. #10
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Inline Members

    Quote Originally Posted by Mybowlcut View Post
    This is how I think of it:
    Code:
    // Declaration.
    class Foo
    {
    public:
        void Func();
    };
    That's not entirely correct. You are defining the class Foo, but only declaring it's member function Func().

    It's easy to check, by the way. You can have multiple declarations of the same thing, but only one definition (if we consider only one compilation unit, at least).
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  11. #11
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Inline Members

    Quote Originally Posted by D_Drmmr View Post
    That's not entirely correct. You are defining the class Foo, but only declaring it's member function Func().
    I think that it can go either way... it is a declaration because you're only declaring the functions (interface), but it is also a definition because you are listing data members (implementation). I prefer to think of it as a declaration though, because the defining (function behaviour and data initialisation) is generally done elsewhere (unless I'm using templates in which case I tend to define everything at the point of declaration).

    Quote Originally Posted by D_Drmmr View Post
    It's easy to check, by the way. You can have multiple declarations of the same thing, but only one definition (if we consider only one compilation unit, at least).
    Easy to check what? Multiple declarations of the same thing? You mean like multiple forward declarations?
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  12. #12
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Inline Members

    Quote Originally Posted by Mybowlcut
    I think that it can go either way... it is a declaration because you're only declaring the functions (interface), but it is also a definition because you are listing data members (implementation). I prefer to think of it as a declaration though, because the defining (function behaviour and data initialisation) is generally done elsewhere (unless I'm using templates in which case I tend to define everything at the point of declaration).
    It is a class definition that contains a member function declaration. Because a class definition is also a class declaration, it is also a class declaration. If you think of it as a class declaration only, then it implies that you think that you cannot include the header that contains this class declaration and then instantiate a Foo object, because the Foo class has not yet been defined at that point.
    Last edited by laserlight; June 10th, 2009 at 12:06 PM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  13. #13
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Inline Members

    Quote Originally Posted by Mybowlcut View Post
    Easy to check what? Multiple declarations of the same thing? You mean like multiple forward declarations?
    I mean this is possible:
    Code:
    class foo;
    class foo;
    Whereas this is not:
    Code:
    class foo
    {};
    class foo
    {};
    Hence, the class is defined twice in the last code, whereas it is declared twice in the first code. That's what I meant with 'checking'; you can use the compiler to check your understanding.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  14. #14
    Join Date
    Sep 2008
    Posts
    113

    Re: Inline Members

    I'm glad my topics generate some discussion. I had previously been interpreting it the same way Mybowlcut is, because, at a glance, that seemed to fit the way the words are used in other aspects of the language (functions, variables, structs, etc). However, I can completely understand why it's a function definition, thus also a function declaration.

    What I decided to do about my situation is simple. For the code that I'm writing based off this book/source, I'm leaving the method definitions in the header file, but outside of the class definition. Makes me more comfortable, and seems cleaner to me. This way, you can quickly look through the class to see what's in it before looking at any method definitions.

  15. #15
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Inline Members

    Quote Originally Posted by laserlight View Post
    It is a class definition that contains a member function declaration.
    Ok, fair enough.
    Quote Originally Posted by laserlight View Post
    Because a class definition is also a class declaration, it is also a class definition.
    Typo? Doesn't make sense to me though, even if I relate it to D_Drmmr's example; perhaps it's just the phrasing?
    Quote Originally Posted by laserlight View Post
    If you think of it as a class declaration only, then it implies that you think that you cannot include the header that contains this class declaration and then instantiate a Foo object, because the Foo class has not yet been defined at that point.
    I don't know enough about compilation etc. to really talk about stuff like this, but isn't the .cpp merged with the .h at compilation? E.g. the definition is always guaranteed to be complete by the time you include it (unless you have circular dependencies or haven't defined functions/statics/etc)? Hence making it a definition? I think I see what you're saying, though, and it does make sense.

    Quote Originally Posted by D_Drmmr View Post
    I mean this is possible:
    Code:
    class foo;
    class foo;
    Whereas this is not:
    Code:
    class foo
    {};
    class foo
    {};
    Hence, the class is defined twice in the last code, whereas it is declared twice in the first code. That's what I meant with 'checking'; you can use the compiler to check your understanding.
    Nothing like a good code sample... I get it now haha.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured