tags:

views:

78

answers:

3

Hi,

I have a string& parameter, and i want to assign it to string parameter.

How can it be done.

+1  A: 
void f(string &param);

string myString = "something";

f(myString)

Is that what you need?

Pavel Radzivilovsky
@Pavel thnx i got it...
iSight
A: 

Generally speaking, string& or even const string& is just a name alias and if you have it somewhere in your code, it's equal to

// This means you just create a new ALIAS named "aliased_string"
// for REAL "real_string"
string& aliased_string = real_string;

Now, whenever you make something with your aliased_string, the changes are done directly with your real_string.

Hence, assigning becomes very simple:

// Create a new string
string constructed_string;

// Means the same as "constructed_string = real_string"
constructed_string = aliased_string;

Note, that if you have some function with reference in it's parameter list, the logic remains the same:

void foo (string& aliased_string) { ... }

generally means the same as

void foo () { string& aliased_string = real_string }

where real_string is passed as a parameter.

Kotti
A: 

If you mean reassigning a string reference then it cannot be done

string &stringref = a_string;

stringref = &b_string; // Cannot be done
David Sykes