473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

What does & here mean? -

QQ
SpreadsheetCell(const string& initialValue);
I don't understand what & means here?

Thank you very much!

A C++ beginner

Nov 22 '05 #1
8 3605
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
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
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
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

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
"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
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
Thank you. One of the problems of not being able to edit the posts. :)

Nov 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andrey Mishenin | last post by:
While accessing MySQl data base by means of perl the value of variable $! sometimes equals " Resource temporarily unavailable". At the mean time everything works correct. What does that mean?...
2
by: pembed2003 | last post by:
Hi all, I recently saw a piece of code that looks like: class one{public: one(){} }; class two : public virtual one{public: two(){} }; class three : virtual public one{public: three(){} }; ...
4
by: pete | last post by:
I found it in the view source of a corporate website. <script Language="Javascript"> <!-- var keyMacro= ]; //--> </script>
6
by: **Developer** | last post by:
What does this mean: External component has thrown an exception. My program crashes after it leaves a subroutine. What I see during debugging is when I press F11 at the End Sub statement
2
by: tony | last post by:
Hello!! I know what an abstract class is which mean that the one of the derived class must define the abstract methods in the abstract class. So all the abstract methods in the abstarct class...
10
by: tony | last post by:
Hello!! I have some demo programs written in C# and they have this construction "" see below. I haven't seen this before so what does it mean ? public bool ShowDropDownButtons { get {...
9
by: JoeC | last post by:
m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth; m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight; What does this mean? I have seen v=&var->member.thing; but what does it mean when you...
0
by: steve | last post by:
What does it mean to set a value or April fools all year long :) http://beyondsql.blogspot.com/2008/03/sql-what-does-it-mean-to-set-value.html
14
by: Tony | last post by:
Hello! It says "Another limitation that you need to be aware of is that using the operator == and != are only permitted when comparing a value of a type supplied to a generic type to null....
1
by: rasmidas | last post by:
Hi, Could anyone please let me know what does it mean by the following statement in solaris shell scripting. $PROMPT " Select an option: " read ans db_option=${ans:=0} I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.