*About inline member function*

This is a discussion on *About inline member function* within the C++ Programming forums, part of the General Programming Boards category; I was told that (i) Every member function defined in the class body is an automatic inline function. (ii) Every ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    *About inline member function*

    I was told that
    (i) Every member function defined in the class body is an automatic inline function.
    (ii) Every member function defined outside of the class body, but in the head file should be declared as inline.
    (iii) Every member function defined outside of the class body, and in the cpp file should NOT be declared as inline.


    Which is true?

    For eg.:

    Code:
    //xxx.h
    Class C1{
    public:
    C1(aa=1):a(aa){};
    int foo1(){ return a;};
    int foo2();
    int foo3();
    private:
    int a;
    };
    
    inline C1::foo2(){
    return 0;
    }
    
    //xxx.cpp
    C1::foo3(){
    return 0;
    }
    Is foo1 an inline function? Is it a must to declare foo2 as an inline function?
    Can we declare foo3 as an inline function?
    Last edited by meili100; 04-16-2007 at 04:40 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Create two source files (say f1.cpp and f2.cpp) which include xxx.h, and use your foo functions.

    Then compile your f1.cpp f2.cpp and xxx.cpp files to see what sort of message(s) you may get by making various things either inline or not inline.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thank you. But how can I tell if foo1 is inline or not?

    Quote Originally Posted by Salem View Post
    Create two source files (say f1.cpp and f2.cpp) which include xxx.h, and use your foo functions.

    Then compile your f1.cpp f2.cpp and xxx.cpp files to see what sort of message(s) you may get by making various things either inline or not inline.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The compiler can generally choose on its own whether a function is inline or not regardless of whether you mark it as inline.

    However, by implementing foo1 inside the class definition you are asking the compiler to inline it, so the answer is that (1) is true, although I'm not sure how you would test that because I don't think you can un-inline something in the class definition.

  5. #5
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    As a rule of thumb, assume that your compiler will ignore your use of the 'inline' keyword or other equivalents completely. Assume that it will pay attention to its compiler-specific ways of saying "please compiler, pretty please inline this function for me," though.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    saw this years ago... still think it's funny
    Code:
    int fibo(int n){
    
     if(n<=1) return 0;
    
     if(n==2) return 1;
    
     return fibo(n-1)+fibo(n-2);
    } 
    
    inline int fibo(int n); // please inline it
    __inline int fibo(int n); // pretty please it
    __forceinline int fibo(int n); // no, seriously. INLINE IT!!
    recursive_my_ass_just_inline_the_damn_thing int fibo(int n); // b@$tARD!!!!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > Thank you. But how can I tell if foo1 is inline or not?
    That wasn't your question, you asked what was necessary to make your program syntactically valid.

    To which the answer is try and compile the code, and then make various 'foo' functions either inline or not inline.

    What the compiler / linker finally decide to do about it is far too implementation specific.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21