What does & here mean? -

Discussion in 'C++' started by QQ, Nov 18, 2005.

  1. QQ

    QQ Guest

    SpreadsheetCell(const string& initialValue);


    I don't understand what & means here?

    Thank you very much!

    A C++ beginner
     
    QQ, Nov 18, 2005
    #1
    1. Advertising

  2. Thomas J. Gritzan, Nov 18, 2005
    #2
    1. Advertising

  3. QQ wrote:
    > SpreadsheetCell(const string& initialValue);
    >
    >
    > I don't understand what & means here?


    'initialValue' is a _reference_ [to a constant 'string']. Read about
    references in your favourite C++ book.

    V
     
    Victor Bazarov, Nov 19, 2005
    #3
  4. QQ

    Alan Guest

    For now think of it in terms of passing values to functions. There are
    two ways to do this. Pass by value and pass by reference. Pass by
    value takes the value of a variable and passes it to another variable
    to be used within a function. Like this:

    //prototype
    int function(int)

    int main()
    {
    //initalize
    int var1 = 0;

    //call function
    var1 = function1(var1);

    cout<< var1 //var1 will output 2
    return 0;
    }
    int function1( int variable )
    {
    variable = 2;
    return variable;
    }

    Pass by reference doesn't pass the value to a new variable, instead the
    same variable(s) are used within the function, so that any changes that
    happen to the variables within the function, affect the variables from
    the main function
    //prototype
    void function(int&)

    int main()
    {
    //initalize
    int var1 = 0; //after function1 executes var1's value will change to
    2

    //call function
    function1(var1);

    cout<< var1 //var1 will output 2
    return 0;
    }
    int function1( int& variable )
    {
    variable = 2;
    return;
    }

    The '&' ampersand is used in conjunction with a variable to talk about
    it's address in memory. That's all variables really are, aliases for
    addresses in memory. So even though these two programs up above output
    the same, their solutions are different. The first one (pass by value)
    copies the value of the variable to a new variable to be used within
    the function, so any change to the variable within the function will
    not affect the origianl one. The second one (pass by reference) is not
    creating a new variable but instead uses the one initalized in the main
    function, so any change within the function directly changes that
    variable. It's really the only way to return multiple values from a
    main function.

    It's not an easy concept to learn but it's vitally important when you
    start learning about pointers.
     
    Alan, Nov 20, 2005
    #4
  5. QQ

    Alan Guest

    a typo I made: the last sentance of the second to last paragraph should
    say "It's really the only way to return multiple values from a
    function."
     
    Alan, Nov 20, 2005
    #5
  6. QQ

    BobR Guest

    Alan wrote in message
    <>...
    >a typo I made: the last sentance of the second to last paragraph should
    >say "It's really the only way to return multiple values from a
    >function."
    >


    Not the only typo.

    // -- reference example --
    // prototype
    // void function(int&)
    // int function1( int& variable )
    void function1( int& variable ){
    variable = 2;
    return;
    }

    int main(){
    //initalize
    int var1 = 0; //after function1 executes var1's value will change to 2

    //call function
    function1(var1);

    cout<< var1 //var1 will output 2
    return 0;
    }

    --
    Bob R
    POVrookie
     
    BobR, Nov 20, 2005
    #6
  7. QQ

    Jim Langston Guest

    "Alan" <> wrote in message
    news:...
    >a typo I made: the last sentance of the second to last paragraph should
    > say "It's really the only way to return multiple values from a
    > function."


    I would tend to disagree.

    struct ReturnStruct
    {
    bool Worked;
    int Value;
    }

    ReturnStruct MyFunction( int SomeVal )
    {
    ReturnStruct ReturnThis;
    if ( SomeVal > 10 )
    ReturnThis.Worked = true;
    else
    ReturnThis.Worked = false;

    ReturnThis.Value = SomeVal * 10;

    return ReturnThis;
    }

    Although I will admit that is awfually horrible looking code. But it is in
    fact another way.
     
    Jim Langston, Nov 20, 2005
    #7
  8. QQ

    Alan Guest

    Considering this person is a beginner, I thought it better to display
    the simplest manner in which the concept is used. While there are more
    complicated manners, I don't know how deep his knowledge is in C++ or
    programming in general.
     
    Alan, Nov 20, 2005
    #8
  9. QQ

    Alan Guest

    Thank you. One of the problems of not being able to edit the posts. :)
     
    Alan, Nov 20, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Li Ma
    Replies:
    1
    Views:
    2,363
    Roedy Green
    Mar 9, 2009
  2. Rahul
    Replies:
    4
    Views:
    624
    Robert Kern
    Apr 7, 2009
  3. C Barrington-Leigh
    Replies:
    1
    Views:
    1,280
    Tim Leslie
    Sep 10, 2010
  4. George Hester

    Try over here likely more to the point here

    George Hester, Sep 30, 2004, in forum: Javascript
    Replies:
    0
    Views:
    124
    George Hester
    Sep 30, 2004
  5. FAQ server
    Replies:
    0
    Views:
    165
    FAQ server
    Aug 10, 2006
Loading...

Share This Page