0

Currently learning c++ and nowhere else better to ask something than to the experts of S.O. I Couldn't find more complete and better answers than here. So there it goes.

DWORD dw = 5;
cout << &dw;

Displays the address where the value of dw is stored.

But then why:

void Display(  DWORD &dwUserId )
{
    cout << dwUserId;
}

int _tmain( int argc, _TCHAR* argv[] )
{
    DWORD dw = 5;
    Display( dw );
} 

Why on this example it is displayed the value of dw and not dw address?

Vinícius
  • 15,498
  • 3
  • 29
  • 53

3 Answers3

4

& has two different meanings in this context:

  • placed in a declaration, it means the variable is a reference. In your case, you pass the parameter by reference.

  • outside a declaration, before a variable, it takes its address.

Besides these, it can also mean the bitwise AND operator

int x;
int& y = x;  //y is a reference to x
int* z = &x; //& takes the address of x and assigns it to z
y & x;       //bitwise AND
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • What would happen if i passed it on a function normally? `void Display( DWORD dwUserId )` what's the difference? – Vinícius Jul 14 '12 at 18:56
  • 2
    @Amani you no longer pass a reference, but a value, so inside the function you have a copy of the original. Look up pass-by-reference vs pass-by-value. – Luchian Grigore Jul 14 '12 at 18:56
  • I've understood, I'm not passing `dw` but the value of `dw`. By passing a reference I'm passing the object itself so the stack doesn't create a copy which can be faster and should be used if passing the value only isn't intended. – Vinícius Jul 14 '12 at 19:00
1

DWORD &dwUserId as a parameter to a function is a reference to DWORD.

In C++, the & operator does two things, depending on context:

  • When used in an r-value expression, it returns the address-of a value. e.g. void* ptr = &myObject;
  • When used in a type specifier it modifies the type to be a "reference-of" type. This is similar to how references work in Java and .NET. e.g. int& ref = myInt;

In your case, your dwUserId parameter is actually of type "reference to DWORD".

If you want to change it to get the address then do this:

void Display(DWORD* userId) {
    cout << userId;
}

Display( &dw );

Also, please void TCHAR types. MBCS is deprecated. Win32 Unicode and wchar_t is the future :) Also avoid Hungarian notation. This isn't the 1980s. We have IDEs now.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • It doesn't only do two things, and inside a declaration it's not an operator. See my answer. – Luchian Grigore Jul 14 '12 at 18:58
  • Dang, I forgot about the bitwise operator. – Dai Jul 14 '12 at 18:58
  • I like the part with the 80's. :)) +1 – Luchian Grigore Jul 14 '12 at 19:00
  • Isn't `TCHAR`, `typedef wchar_t TCHAR`? What hungarian notation has to do with IDE's? – Vinícius Jul 14 '12 at 19:04
  • 1
    @Amani `TCHAR` is defined as `wchar_t` only if UNICODE is defined. – Luchian Grigore Jul 14 '12 at 19:05
  • Oh thanks, and what about hungarian notation has to do with IDE's?? if you are on a 500.000 code line source and you do a pointer on VS `pointer->` and InteliSense shows the class members it's much easier to find out what type is what – Vinícius Jul 14 '12 at 19:07
  • Hungarian notation was used a lot before we had IDEs that could semantically analyze our code, so variables needed to be self-documenting. Now we can just mouse-over a variable to find its type, thus making prefixes like "sz" and "dw" obsolete. And actually "dw" is technically incorrect because on modern platforms a "word" is 64 or 32 bits, not 16. – Dai Jul 14 '12 at 19:08
  • I'm sorry but it makes code much easier to read, specially on extremely long functions where you forget what type is what and you just want to read it instead of mouseovering variable by variable to know what they are, spank me please but I don't get what "dw"(double word) notation has to do with 16 bits, as you've said they are either 64 or 32 and can be shifted. – Vinícius Jul 14 '12 at 19:13
0

Because when you use the & sign before a parameter name on a function definition you are telling the compiler to pass that parameter as a reference, i.e., don't make a copy of it.

When you use it before a variable name somewhere in the code you are telling the compiler to use the variable address instead of his value.

fjsabai
  • 119
  • 2