Public Group

# References

This topic is 4561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, Im trying to work with references, and the thing im trying to do itsnt working. Im trying to change the object my reference is refering to. Source:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

string display(vector<string>& vec, int i);

int main()
{
vector<string> inventory;
inventory.push_back("M4");

cout << "Inventory:\n\n";

string& rStr = display(inventory, 0);
cout << "1: " << rStr << endl;

string Str = display(inventory, 1);
cout << "2: " << Str << endl;

rStr = "Healing Potion";
cout << "\nChanged the first item to: " << inventory[0] << endl;

return 0;
}

string display(vector<string>& vec, int i)
{
return vec;
}


##### Share on other sites
string display(vector<string>& vec, int i){	return vec;}

You are returning a copy of vec, not a reference to it.

CM

##### Share on other sites
A reference once created, will always refer to the same object; you can't change which object a reference refers to.

##### Share on other sites
Quote:
 Original post by NUCLEAR RABBITIm trying to change the object my reference is refering to.

Where? Show us what you've tried, we'll show you your mistake(s).

Anyway, rStr is a reference to the string returned by display(), which is a copy of the string contained in the vector because display() returns by value.

As Conner said...

##### Share on other sites
I don't see where your changing the object your referencing. Your passing your vector inventory by reference to the function and then returning the string that is held in the vector. Now say you wanted to switch item one for item zero in the function display. You just do this.

std::string tmp = vec[0];
vec[0] = vec[1];
vec[1] = tmp;

Now your vector would have been changed because you passed by reference.

##### Share on other sites
what i ment was, is that rStr is a reference to the first element in inventory. In my book, it says that when you assign a reference, whatever you do to the reference, happenes the to the thing it is refering to.

So, rStr is refereing to Nade. Now, in the last couple lines of main(), i try changing healing potion. Why doesnt it change inventory[0] to healing potion if thats what its refering to?

##### Share on other sites
Quote:
 Original post by Conner McCloudYou are returning a copy of vec, not a reference to it.

##### Share on other sites
Quote:
 Original post by NUCLEAR RABBITSo, rStr is refereing to Nade. Now, in the last couple lines of main(), i try changing healing potion. Why doesnt it change inventory[0] to healing potion if thats what its refering to?

As has already been mentioned rStr my say "Nade", but it refers to a COPY of inventory[0], so changing it does not change inventory[0].

It's been a while since I touched C++, but I think you need to have the function return a reference.

string& display(vector<string>& vec, int i){	return vec;}

Edit: What compiler are you using, it must really suck. G++ gave me an error when trying to compile you're code: "invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'std::string'".

Edit 2: Yes change the return type to string&, it should work then.

##### Share on other sites
Quote:
Original post by Scet
Quote:
 Original post by NUCLEAR RABBITSo, rStr is refereing to Nade. Now, in the last couple lines of main(), i try changing healing potion. Why doesnt it change inventory[0] to healing potion if thats what its refering to?

As has already been mentioned rStr my say "Nade", but it refers to a COPY of inventory[0], so changing it does not change inventory[0].

It's been a while since I touched C++, but I think you need to have the function return a reference.

string& display(vector<string>& vec, int i){	return vec;}

In my book, it says when you make a reference, you dont get a copy of what its refering to, its just like another name for what its refering to. Also, isnt vec already declared as a reference?

##### Share on other sites
Original post by NUCLEAR RABBIT
Quote:
Original post by Scet
Quote:
 Original post by NUCLEAR RABBITSo, rStr is refereing to Nade. Now, in the last couple lines of main(), i try changing healing potion. Why doesnt it change inventory[0] to healing potion if thats what its refering to?

As has already been mentioned rStr my say "Nade", but it refers to a COPY of inventory[0], so changing it does not change inventory[0].

It's been a while since I touched C++, but I think you need to have the function return a reference.

string& display(vector<string>& vec, int i){	return vec;}

Oh ok, i found the problem, i had to declare the function as a reference! [lol]

string& display(vector<string>& vec, int i);

Thanx for all the help everyone. [smile]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 15
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
634151
• Total Posts
3015819
×