References and function calls

This is a discussion on References and function calls within the C++ Programming forums, part of the General Programming Boards category; Hi We know that the variables when making a function call are copied using assignment, i.e. in Code: void test(int ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    140

    References and function calls

    Hi

    We know that the variables when making a function call are copied using assignment, i.e. in

    Code:
    void test(int a)
    {
    cout << a;
    }
    
    int main()
    {
    test(3)
    return 0;
    }
    we have

    Code:
    int a;
    a=3;
    But what if I had chosen to call test using a reference? How would the reference be passed to the function test? I'm thinking of the fact that references need to be initialized.

    Best,
    Niles.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    If you tried it with a non-const reference parameter, you should get a compile error. If you tried it with a const reference parameter, it should work just fine.
    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
    Aug 2009
    Posts
    140
    Quote Originally Posted by laserlight View Post
    If you tried it with a non-const reference parameter, you should get a compile error. If you tried it with a const reference parameter, it should work just fine.
    But how is it passed? If I had

    Code:
    void test(int &a)
    {
    cout << a;
    }
    
    int main()
    {
    int b=3;
    test(b)
    return 0;
    }
    ,

    then it does not get passed as

    Code:
    int a&; //ERROR
    a=b; //ERROR
    Then how does it get passed by assignment?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    With a const reference parameter, the reference is initialised to refer to the argument even if the argument is a literal. It is quite safe since due to the constness you would not be modifying the literal through the reference.
    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

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    I'm gonna guess:
    Code:
    int & a = b;
    Not 100% sure what you're asking here about "how it gets passed". It's almost like you're asking how the assignment/initialization of the reference parameter happens.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by laserlight View Post
    If you tried it with a non-const reference parameter, you should get a compile error. If you tried it with a const reference parameter, it should work just fine.
    No it shouldn't!!
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by hk_mp5kpdw View Post
    I'm gonna guess:
    Code:
    int & a = b;
    Not 100% sure what you're asking here about "how it gets passed". It's almost like you're asking how the assignment/initialization of the reference parameter happens.
    That is an initialization; arguments in C++ are passed to function parameters by assignment. That is my question: Since references cannot be assigned, but must be initialized, then how can I have a reference as a function argument?

    I am not sure I understand Laserlight's response in light of my question. But you say it gets initialized?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by Sipher
    No it shouldn't!!
    Notice that I was talking with reference to Niels_M's code example in post #1. The code example in post #3 is fine in that respect (though it contains a syntax error), since the variable b rather than the integer literal 3 was passed to test.

    Quote Originally Posted by Niels_M
    arguments in C++ are passed to function parameters by assignment.
    That is false.
    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

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    O_O ... Sorry, my bad. I didn't notice it.
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by laserlight View Post
    That is false.
    ?!

    I've read numerous places that it is the case, the C-FAQ being one of them (as far as I remember).

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by Niels_M
    I've read numerous places that it is the case, the C-FAQ being one of them (as far as I remember).
    Then all those places are wrong, or they are using "assignment" in its more general computer science meaning.
    Quote Originally Posted by C++03 Section 5.2.2 Paragraph 4 (part)
    When a function is called, each parameter shall be initialized with its corresponding argument.
    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

  12. #12
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by laserlight View Post
    Then all those places are wrong, or they are using "assignment" in its more general computer science meaning.
    Ok, thanks for that. Just as a reference: 10.6 Arrays and Pointers as Function Arguments

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. references to function in classes
    By Chrip in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2007, 11:39 AM
  3. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  4. function parameter references
    By Bob001 in forum C++ Programming
    Replies: 3
    Last Post: 06-22-2003, 07:45 AM
  5. error LNK2001: unresolved external symbol
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-12-2002, 08:45 PM

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