Jump to content
  • Advertisement
Sign in to follow this  
NUCLEAR RABBIT

References

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

If you intended to correct an error in the post then please contact us.

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("Nade");
	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 this post


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

Share this post


Link to post
Share on other sites
Quote:
Original post by NUCLEAR RABBIT
Im 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 this post


Link to post
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 this post


Link to post
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 this post


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


Share this post


Link to post
Share on other sites
Quote:
Original post by NUCLEAR RABBIT
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?


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 this post


Link to post
Share on other sites
Quote:
Original post by Scet
Quote:
Original post by NUCLEAR RABBIT
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?


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 this post


Link to post
Share on other sites
Original post by NUCLEAR RABBIT
Quote:
Original post by Scet
Quote:
Original post by NUCLEAR RABBIT
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?


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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!