Short questions...

This is a discussion on Short questions... within the C++ Programming forums, part of the General Programming Boards category; I'm porting from C to C++ (finally ) and I have a few questions that I was unable to find ...

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    Short questions...

    I'm porting from C to C++ (finally ) and I have a few questions that I was unable to find answers to in my text book.

    1.) the inline - I know what it does, but I'm not really sure how to declare it;
    I know that when I write the function I need to do the next:
    Code:
    inline void myclass::function(void)
    {
       ...
    }
    But how will it look like in the function's declaration? Do I need to place inline there as well like this:
    Code:
    class myclass {
    public:
       inline void function(void);
    };
    2.) Is the inline for class functions only, or can I place it on a global functions as well?

    3.) What is the using namespace std; does, and why the text books don't list it in their example programs? Do I really need it?


    Thanks alot.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    An inline function needs to be visible to every translation unit using it. In short, this means you have to define an inline function in your .hpp file (you are using .hpp and not .h, RIGHT?). A defintion is the one with { and }. Consequently, this means you can just define the function with the class itself since class defintions need to be visible to every translation unit too. Finally, any function defined inside the class definition is implicitly inlined. Which means you don't need the inline keyword. Also, inline is *merely* a compiler suggestion. Your compiler is free to completly disregard it. So in otherwords, don't expect much from 'inline'. In general I'd suggest avoiding explicitly inling things since your compiler will probably inline anything that should be regardless. Not to mention optimization shouldn't be your concern at this stage.

    Yes, you do need using namespace std;. If your textbook does not use it then it is not a C++ textbook and you should burn it.

    Get Thinking in C++ if you are cheap and don't want to buy a real book (it is free and online). Otherwise I'd suggest buying Accelerated C++.
    FYI, this board keeps on demanding i use code tags, but since I don't have any code I don't see what it wants me to put in the tags. Perhaps someone should look into that.

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    It probably has something to do with you using curly brackets in your explanation. Of course, a way around this would be to use the phrase 'curly brackets' instead of actually using the character.

    Alternatively, the webmaster could refine the string parsing algo which they obviously use before each thread can be submitted?

    Although you have to appreciate, many bugs exist, be it due to the fact ppl using different browsers or deficiencies in the web source code. I believe there is a sticky about reported bugs in one of the forums?

    Last edited by treenef; 09-03-2005 at 12:57 PM.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    What's the point of using inline functions? Why not just use normal ones? Is it performance/memory usage or something?

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Inlining a function will save that great big overhead time it takes to call a function (push return onto stack, change EIP, execute, pop ret back into EIP and continue). Of course this really isn't much at all, but in a critical section of your code, the inline could potentially save a bit of time. Anyways, the C++ FAQ-Lite has a little bit of info on defining inline functions

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Quote Originally Posted by Tonto
    Inlining a function will save that great big overhead time it takes to call a function (push return onto stack, change EIP, execute, pop ret back into EIP and continue). Of course this really isn't much at all, but in a critical section of your code, the inline could potentially save a bit of time. Anyways, the C++ FAQ-Lite has a little bit of info on defining inline functions
    So basically inline functions are that of not processing it THEN computing it, it just reads it and computes it on the fly? Am I wrong?

  7. #7
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    A short code quote from the FAQ link given above by Tonto
    Code:
    class Foo {
     public:
       void method();  ← best practice: don't put the inline keyword here
       ...
     };
    Why not ?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Quote Originally Posted by The same FAQ I linked
    From a practical standpoint, this separation makes life easier and safer for your users. Say Chuck wants to simply "use" your class. Because you read this FAQ and used the above separation, Chuck can read your class's public: part and see everything he needs to see and nothing he doesn't need to see. His life is easier because he needs to look in only one spot, and his life is safer because his pure mind isn't polluted by implementation minutiae.

    Back to inline-ness: the decision of whether a function is or is not inline is an implementation detail that does not change the observable semantics (the "meaning") of a call. Therefore the inline keyword should go next to the function's definition, not within the class's public: part.

    NOTE: most people use the terms "declaration" and "definition" to differentiate the above two places. For example, they might say, "Should I put the inline keyword next to the declaration or the definition?" Unfortunately that usage is sloppy and somebody out there will eventually gig you for it. The people who gig you are probably insecure, pathetic wannabes who know they're not good enough to actually acomplish something with their lives, nonetheless you might as well learn the correct terminology to avoid getting gigged. Here it is: every definition is also a declaration. This means using the two as if they are mutually exclusive would be like asking which is heavier, steel or metal? Almost everybody will know what you mean if you use "definition" as if it is the opposite of "declaration," and only the worst of the techie weenies will gig you for it, but at least you now know how to use the terms correctly.
    They grand FAQ wouldn't just leave you with a "because it is", they explain it too

    @blank-stare: inline functions are expanded (all the code is placed into the calling function) at compile time, saving to function call. This is somewhat analogous to a #define like function, however, there are differences. I suggest reading the linked C++ FAQ-Lite entries for these.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03:55 AM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. Say what? - Weird error.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2006, 12:54 AM
  4. Help with mult-dim arrays and pointers to them
    By skybolt_1 in forum C Programming
    Replies: 11
    Last Post: 05-02-2003, 12:47 PM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 10:17 AM

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