By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,505 Members | 1,761 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,505 IT Pros & Developers. It's quick & easy.

What does & here mean? -

P: n/a
QQ
SpreadsheetCell(const string& initialValue);
I don't understand what & means here?

Thank you very much!

A C++ beginner

Nov 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
QQ schrieb:
SpreadsheetCell(const string& initialValue);
I don't understand what & means here?


It means that initialValue is a reference (an alias) to a (const) string:

http://www.parashift.com/c++-faq-lite/references.html

Thomas
Nov 22 '05 #2

P: n/a
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
Nov 22 '05 #3

P: n/a
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.

Nov 22 '05 #4

P: n/a
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."

Nov 22 '05 #5

P: n/a

Alan wrote in message
<11**********************@f14g2000cwb.googlegroups .com>...
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
Nov 22 '05 #6

P: n/a
"Alan" <be*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.
Nov 22 '05 #7

P: n/a
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.

Nov 22 '05 #8

P: n/a
Thank you. One of the problems of not being able to edit the posts. :)

Nov 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.