0

Possible Duplicate:
References in C++
What does int & mean

I am having difficulty understanding the statement int &f = d in the following code. I would appreciate it if someone could clear this up

int main()
{ 
   int d = 13 ;
   int &f = d ;
   std::cout << f;
}

Edit : I am familiar with the concept of references sorry I didnt give enough background. I know that if i do something like this

int a = 12
std::cout << &a //this gives the address of a and not the content of the address which is 12

Now my issue with int &f = d The problem to me is f seems to require an address whereas d is giving it a value not an address.

Community
  • 1
  • 1
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
  • If you have difficulty understanding it, it's time to spend some quality time with your favourite C++ text book (see the FAQ if you don't have one) or course notes. – Kerrek SB Oct 25 '12 at 00:30
  • instead of copying the answer, just see this question: http://stackoverflow.com/questions/1409775/what-is-the-difference-between-references-and-normal-variable-handles-in-c – Stefan Oct 25 '12 at 00:30
  • http://www.cprogramming.com/tutorial/references.html – Faruk Sahin Oct 25 '12 at 00:32
  • 1
    @KeithNicholas: Hmm.. the question isn't "could someone please explain the concept of references in C++", but rather, "what is this piece of syntax". :-( I think that qualifies for "does not show any research effort". (And by the way, who would *upvote* this?!) – Kerrek SB Oct 25 '12 at 00:32
  • Firstly, it's much clearer as `int& f = d;`. Secondly, what happened to your C++ book? Dog ate it? – Lightness Races in Orbit Oct 25 '12 at 00:39
  • @LightnessRacesinOrbit you can use either int& f or int &f. Wish I had a dog – Rajeshwar Oct 25 '12 at 00:52
  • @KeithNicholas just updated my question – Rajeshwar Oct 25 '12 at 00:52
  • @Rajeshwar: Symbol `&` in that declaration has absolutely nothing to do with "address of" operator and addresses in general. So, stop making guesses based on "what it looks like" to you and stop comparing it to `&` operator. It is meaningless. Read your favorite C++ book's chapter on references. Your edit that states that you are "familiar with the concept of references" shows that you are actually *not* familiar with that concept. You must be thinking about pointers. Pointers and references are different things in C++. – AnT stands with Russia Oct 25 '12 at 00:55
  • @AndreyT What does the reference to operator "&" return ? I believe it returns an address right. Jeez.. talk about being rude – Rajeshwar Oct 25 '12 at 00:59
  • @Rajeshwar: "...the reference to operator `&`..." - I don't know what you mean by this. – AnT stands with Russia Oct 25 '12 at 01:00
  • The only confusion I am having with this statement is that int &f should be given a pointer value instead its getting a non-pointer – Rajeshwar Oct 25 '12 at 01:01
  • @Rajeshwar: **No.** I don't know where you get this. `int *f` would need a pointer value as an initializer. Meanwhile, `int &f` is a completely different thing, which needs an `int` Lvalue as an initializer. Do you see the difference between `int *f` and `int &f`? The first is a pointer. The second is a reference. – AnT stands with Russia Oct 25 '12 at 01:04
  • @Rajeshwar: Yes, I'm quite aware that either is valid. But one is [clearer](http://kera.name/articles/2010/05/tomalaks-monday-monstrous-rant-i-align-your-asterisks/) than the other. – Lightness Races in Orbit Oct 25 '12 at 08:46

1 Answers1

1

I could write, and write, but better answer is here: http://en.wikipedia.org/wiki/Reference_(C++)

In general, in C++ it is something like a pointer. f is used as something that refers to what d is.

Majid Laissi
  • 19,188
  • 19
  • 68
  • 105
Grzegorz
  • 3,207
  • 3
  • 20
  • 43