7 Replies - 382 Views - Last Post: 21 November 2012 - 10:37 AM Rate Topic: -----

#1 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

"&" question

Posted 20 November 2012 - 07:53 PM

I saw somebody put the & after the variable or whatever you call it rather than before, what is the difference? Is it right to put it after? What does it do? I can't find an answer to this question so I thought I'd ask. (i.e:

void initialize_board(player&) is what I saw.

why not (&player)?
Is This A Good Question/Topic? 0
  • +

Replies To: "&" question

#2 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: "&" question

Posted 20 November 2012 - 08:03 PM

This was C++ source code, I wager, and player was not a variable name but type name.
That line can be read as "a declaration of function named initialize_board, returns void, takes one parameter of type reference to player". You don't have to name parameters in function declaration, and here there is no parameter name given. It's legal.

See pass by reference and introduction to references by v0rtex.

Also read C++ FAQ Lite entry on references.

This post has been edited by Xupicor: 20 November 2012 - 08:05 PM

Was This Post Helpful? 1
  • +
  • -

#3 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: "&" question

Posted 20 November 2012 - 08:08 PM

I kinda understand. But would &player be the same thing? Or what is the diff? Try to explain it in dummy language?
Was This Post Helpful? 0
  • +
  • -

#4 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: "&" question

Posted 20 November 2012 - 08:18 PM

Ok, an example:
void foo(int ); // declaration (no body), parameter name omitted like in the OP, however I don't like that style - parameter names provide useful information in headers 

void foo(int i) { // definition 
    i = 5; // i is a copy of passed in parameter, it's local - it doesn't affect passed in variable
}

void bar(int& ); // declaration, again, parameter name omitted even though it's bad style IMO

void bar(int& i) { // i is a reference to parameter passed in
    i = 5; // original passed in variable gets modified!
}

//
int data = 0;
foo(data);
// here data == 0
bar(data);
// here data == 5 ! pass by reference means you can modify the original passed variable



Now, how would
void foo (&int );
make any sense? ;) What would that be, "an int to reference"? ;)
Was This Post Helpful? 2
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,564
  • Joined: 09-June 09

Re: "&" question

Posted 20 November 2012 - 10:45 PM

The '&' can have three possible uses in C++.

'&' can be used to declare a reference
int x = 10;
int &y = x;



'&' can be used as a bitwise and operator
int x = 0x01;
int y = 0x02;
int z = x & y; // = 0x01 & 0x02 = 0x01



'&' can be used as an address operator
int x = 5;
std::cout<<"x value: "<<x<<"  memory location: "<<&x<<std::endl;



The fourth is essentially a use of the first, but it is the most common
'&' can be used to pass by reference
void getInput(int &x) {
   std::cin>>x;
}

int main() {
   int x;
   getInput(x);


Was This Post Helpful? 3
  • +
  • -

#6 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: "&" question

Posted 21 November 2012 - 10:18 AM

View Postjjl, on 20 November 2012 - 10:45 PM, said:

The '&' can have three possible uses in C++.

'&' can be used to declare a reference
int x = 10;
int &y = x;



'&' can be used as a bitwise and operator
int x = 0x01;
int y = 0x02;
int z = x & y; // = 0x01 & 0x02 = 0x01



'&' can be used as an address operator
int x = 5;
std::cout<<"x value: "<<x<<"  memory location: "<<&x<<std::endl;



The fourth is essentially a use of the first, but it is the most common
'&' can be used to pass by reference
void getInput(int &x) {
   std::cin>>x;
}

int main() {
   int x;
   getInput(x);



I understand kinda but you didn't include player& where & is at the end, which is my original question?
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,549
  • Joined: 23-August 08

Re: "&" question

Posted 21 November 2012 - 10:30 AM

Which was answered by Xupicor in the very first reply.
Was This Post Helpful? 0
  • +
  • -

#8 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: "&" question

Posted 21 November 2012 - 10:37 AM

View PostJackOfAllTrades, on 21 November 2012 - 10:30 AM, said:

Which was answered by Xupicor in the very first reply.


I understand that JackOfAllTrades. I was just giving feedback to the person who last replied to my post, that is all. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1