Thread: Some questions again

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Some questions again

    Hallo,

    I have a few question

    1. Why return a reference?

    2. What is the benefits of inline? From reading only I have gathered that it makes the program smaller and bigger. Faster and slower and so on.

    3. When to use the keyword extern?

    4. How do I assign something to a const class member variable?
    Code:
    class Player{
    public:
       Player(std::string n);
       ...
    private:
       const std::string name;
    };
    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by h3ro View Post
    1. Why return a reference?
    Because it avoids creating another copy of the object and it avoids using pointers for simple things. In short, it's more efficient.

    2. What is the benefits of inline? From reading only I have gathered that it makes the program smaller and bigger. Faster and slower and so on.
    Inline makes the code faster, but can make the code bigger.

    3. When to use the keyword extern?
    When you have a variable defined in a source file other than the one it's used in. Since you can't define something twice, and the compiler can't see the a variable in another source file, you must declare it as extern to tell the compiler it exists elsewhere.

    4. How do I assign something to a const class member variable?
    Code:
    class Player{
    public:
       Player(std::string n);
       ...
    private:
       const std::string name;
    };
    You can't. You must initialize it with the initialization list in the constructor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    1. Why return a reference?
    For function chaining, or to allow a member variable to be modified in a syntactically nice way.

    2. What is the benefits of inline? From reading only I have gathered that it makes the program smaller and bigger. Faster and slower and so on.
    Indeed, the benefits depend on the context. It may avoid the overhead of a function call, but may introduce executable code bloat.

    3. When to use the keyword extern?
    See Stroustrup's glossary on extern.

    4. How do I assign something to a const class member variable?
    You don't. You just initialise it in the constructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    thanks

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    To your fourth question, you can assign a static constant data member in a class declaration:

    Code:
    class Foo
    {
    public:
       static const int ARRAY_SIZE = 10;
    ....
    But I may of mis-read what you meant
    Last edited by swgh; 04-17-2008 at 12:26 PM. Reason: Missed "int"
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM