By Value and By Reference

This is a discussion on By Value and By Reference within the C++ Programming forums, part of the General Programming Boards category; Zomg, a project fork lol. Here's a question: what's the difference between these: Code: void a(std::string x) {} and Code: ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    Zomg, a project fork lol.

    Here's a question: what's the difference between these:

    Code:
    void a(std::string x) {}
    and

    Code:
    void a(std::string& x) {}
    ?

    Or do these apply only when I have const before ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Passing by value means a copy is made. Passing by reference does not involve copying. You could read this FAQ on references.
    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

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    another thing is that the second version is able to change the string x references

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by pheres View Post
    another thing is that the second version is able to change the string x references
    Which means that if the purpose of using the reference is only to avoid the copying overhead and not to change the object it refers to, a const reference should be used.

Popular pages Recent additions subscribe to a feed

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