Click here to Skip to main content
15,886,100 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How do I pass a reference of a variable into a vector so that everytime I change it's value in the vector, it would reflect back to the original variable.
C++
int a = 10;
int b = 20;
int c = 30;

std::vector <int> d {a, b, c};


I can already see that I only pass as value to the vector.
how do I make it pass as reference?

What I have tried:

As my knowledge is very shallow. I tried few ways using pointers but none has work so far.
Posted
Comments
Sergey Alexandrovich Kryukov 11-Feb-16 21:11pm    
Reference is not an object, you cannot use it as an object. What is your ultimate purpose? How about using pointers? or why cannot you simply replace the element with another one?
If your knowledge is shallow (your self-criticism is much appreciated and is one of the important prerequisites to success), you need to ask deeper questions; for example, your "but none has work" is not informative. By some reason, you filled in "What I have tried" section. Then why not telling us what you really tried?
—SA
Are Riff 12-Feb-16 7:04am    
after a good sleep, I kinda got it to work with std::vector <int*> d {&a,&b,&c}

my ultimate purpose is to search from the vector for particular values and id them for further processing later. I might use map instead though. I'm not sure
[no name] 11-Feb-16 21:20pm    
You have not framed your question well. The first thing to tell us is what are you trying to do. Why do you need this construct? It seems you are going up the wrong path to begin with.
Philippe Mori 11-Feb-16 22:07pm    
Usually, you don't want to write such code.

Well, one could use pointers but you have to understand what you do. Best to avoid. Why not simple use only the vector?

The short answer is you can't - std::vector has severe problems with references, it was designed to hold values.

So what you need is something that looks like a reference to an int but is actually a value. Something like...

C++
class int_ref
{
	public:
		int_ref( int &ref ) : ref_( ref ){}

		operator int &( )
		{
			return ref_;
		}

	private:
		int &ref_;
};


This looks enough like an int that if you make a vector of them you can change values through them:

C++
auto main()->int
{
	int a = 10, b = 20, c = 30;
	std::vector<int_ref> nums{ a, b, c };

	std::copy( begin( nums ), end( nums ), std::ostream_iterator<int>( std::cout, "\n" ));

	for( auto n : nums ) n+= 10;

	std::copy( begin( nums ), end( nums ), std::ostream_iterator<int>( std::cout, "\n" ) );
}


Now this may or may not be a good idea... int_ref has some subtle behavioural problems that means it's not quite a reference to an int but if all you want to do is modify some other objects through a vector of references to that object then knock yourself out. If you have a use case that needs this sort of thing it might be okay but in general classes with conversion/cast operators like this one are a pain in the bum. They violate most programmers expectations as to how the world works and can be a maintenance nightmare if you don't realise what's going on.
 
Share this answer
 
Comments
CPallini 12-Feb-16 4:25am    
5.
Aescleal 12-Feb-16 4:49am    
Thank you!
In C++, pointers can be references of variables. So, you may use pointer to store the reference of each of the integer value in your vector:
C++
   int a = 10;
int b = 20;
int c = 30;

std::vector<int*> v{ &a, &b, &c };  //line A

cout << "Original values are:" << endl;

for (auto item : v)
{
    cout << *item << endl;
}

(*v[1]) += 10;  // adding 10 to b by ref

cout << "Updated values are:" << endl;
for (auto item : v)
{
    cout << *item << endl;
}


If you think that it's too difficult, try to just think of the case of a single variable:
C++
int* pValue = &other;
and apply the uniform initialization such that you can get line A.

I think you'll need to revise some basic concepts about pointer-of-array and array-of-pointer in the classic C++ before you can jump start your journey to modern C++.
 
Share this answer
 
Comments
CPallini 12-Feb-16 3:06am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900