Thread: What should I really use inline functions?

  1. #1
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140

    Question What should I really use inline functions?

    When should I really use an inline function? I've read contradicting* articles on it. Should it be small and not called alot? Or small and called alot? Would assigning 7 values to an array of 600 elements be a good candidate?
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  2. #2
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    A few general rules ....

    Code inline if .......

    1. The function has a small number of actions.
    2. The function has no loop.
    3. The function has no if statements.
    4. The function has no switch constructs.
    5. The function does not call an external function.


    So, generally if your function fails to meet one of these conditions, prototype it within the class and code it outside of the class.

  3. #3
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    Thankyou, that clears things up a bit.
    But I"m still wondering about how often an inline function should be called? If it has only one action but is called 600 times, would it be a good idea to inline it? What are the consequences/benefits?
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034

    etg592

    Generally if you have a function like

    Code:
    class A {
    public:
           int do_s() { s++; return s; }
    private:
           int s;
    };
    You should inline on the spot if this is in a bottleneck of the program, otherwise you would probably do just as without inlining. I think if it's small and not called alot then it's not taking up alot of cpu time anyways and so you shouldn't inline it?


    Would assigning 7 values to an array of 600 elements be a good candidate?
    Probably not. I think most of the time would be in
    accessing the memory of the array. For cases your unsure
    you should profile and read the assembly listing.

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    The inline keyword is only a suggestion to the compiler. This means simple functions will probably be compiled as inline function during compiler optimisation anyway.

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    When you declare a function inline, the compiler doesn't call the function, it places the functions code in your program so it will seem as if there is no function.

    Eg.
    Code:
    #include <iostream.h>
    
    inline int function(int num){ return num + 1; }
    
    int main()
    {
    	int number;
    	number = function(1);
    	cout << number << endl;
    	return 0;
    }
    So then compiler would replace the function call with number + 1, so the statement would be something like this,

    number = number + 1;

  7. #7
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    WOOO I GET IT! thanks doggy, that helped
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inline Definitions and Declarations?
    By legit in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2009, 01:59 PM
  2. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  3. When to inline your *tors
    By Angus in forum C++ Programming
    Replies: 43
    Last Post: 10-29-2008, 03:38 PM
  4. conditional breakpoints and inline functions
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 08-10-2006, 08:30 PM
  5. inline friend functions.
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 12:37 PM