1

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

How can it be done.

2
  • 6
    Post some code that illustrates what you are asking about. Commented Jun 18, 2010 at 12:05
  • 1
    Do you mean like a function void f(string& a, string b) within which the value of b is assigned to a? Or are you talking about something like string * b as the second parameter, to access that outside f()? Please be clear and supply us with some code. Commented Jun 18, 2010 at 12:13

3 Answers 3

1
void f(string &param);

string myString = "something";

f(myString)

Is that what you need?

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

string &stringref = a_string;

stringref = &b_string; // Cannot be done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.