Some questions again

This is a discussion on Some questions again within the C++ Programming forums, part of the General Programming Boards category; Hallo, I have a few question 1. Why return a reference? 2. What is the benefits of inline? From reading ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    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
    21,409
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,475
    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"
    I'm just trying to be a better person - My Name Is Earl

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, 08: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

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