CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2004
    Posts
    8

    Ampersand usage in C++ ?

    Guys I got a question,

    When you use the Ambersand sign "&" infront of a variable it means we are passing the address of that variable , right?

    what happents when you have the "&" at the end of the variable .
    Ex :
    if I have a function header

    exam(const K&, const V&);

    what is the usgae of & in the above?

    and what is the difference between this one
    exam(const &K, const &V);

  2. #2
    Join Date
    Jun 2001
    Location
    Michigan
    Posts
    2,222
    When you use the Ambersand sign "&" infront of a variable it means we are passing the address of that variable , right?
    Correct,

    Code:
    int x=10;
    int* y=&x;
    In this example we are passing the address of x to the pointer y.

    what happents when you have the "&" at the end of the variable .
    This refers to passing by reference.

    Code:
    int i=1;
    int& r=i;  // r and i now refer to the same int
    int x=r;   // x=1
    r=2;       // i=2
    *Note, references must be initialized. Also important, the reference type and variable type should NOT differ, otherwise c++ will create a hidden object that does not alias the specified value.

    Code:
    int value;
    float& alias=value;
    Very bad, the types differ.

    Code:
    int value;
    int& alias;
    Also bad, alias is not initalized.

    There are reasons for using references, one is that they use a cleaner syntax than passing pointers.

    When you pass a reference to a function, you eliminate copying. So if you have a very large object and don't want to modify it in the function, passing it as a

    Code:
    const T&
    is a good idea.
    Last edited by bluesource; January 26th, 2004 at 11:15 AM.

  3. #3
    Join Date
    Jun 2002
    Location
    Chengdu, China
    Posts
    60
    The first statement

    exam(const K&, const V&);

    is a valid funtion declartion if K and V are types. This function's parameters are refenence.

    However the second statement

    exam(const &K, const &V);

    I do not understand. const is a key word and if K and V are types then why & is positioned before a type£¿ It is illegal against c++ grammer.

  4. #4
    Join Date
    Jun 2001
    Location
    Michigan
    Posts
    2,222
    exam(const &K, const &V);

    I do not understand. const is a key word and if K and V are types then why & is positioned before a type£¿ It is illegal against c++ grammer.
    That is a good point, I was confused by this part of the question.
    The OP was confused, in the 1st posting Luda says:
    what happents when you have the "&" at the end of the variable .
    when Luda probably meant: "what happens when you have the "&" at the end of the type."


    Likewise, Luda probably meant
    Code:
    exam(const K &value, const V &value2);
    which is the same thing as:

    Code:
    exam(const K& value, const V& value2);
    So to answer the final question, what is the usage of exam(const K&, const V&); ?

    Take the following example:
    Code:
    exam(const K&, const v&);
    
    K a=10;
    K& b=a;
    
    V c=11;
    V& d=c;
    
    exam(b,d);
    This illustrates passing by reference, your function exam will modify the a and c variables by reference, even though you passed b and d in.
    Last edited by bluesource; January 26th, 2004 at 11:10 AM.

  5. #5
    Join Date
    Jun 2001
    Location
    Michigan
    Posts
    2,222
    I hope this wasn't overly confusing...but now after I've thought about it I think your confusion can be summarized as follows:

    Code:
    int x=10;
    int& y=x;
    We say y references x.

    But this is saying the same thing as

    Code:
    int x=10;
    int &y=x;
    Once again we say y references x even though they were declared differently. The & does not refer to the address.
    So

    Code:
    int x=10;
    int* y=&x;
    Here the & refers to the address, whereas in the above two examples it does NOT.
    Last edited by bluesource; January 26th, 2004 at 12:05 PM.

  6. #6
    Join Date
    Jan 2004
    Posts
    8

    Thanks!

    Thanks guys!

    I guess my question was a lil confusing. sorry.

    but Im all clear now.

  7. #7
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Thanks!

    Originally posted by LudaLuda
    I guess my question was a lil confusing. sorry.
    Don't worry, the answers have been not less confusing...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured