Sign in to follow this  
thedodgeruk

passing stl , by reference?

Recommended Posts

thedodgeruk    124
simple example
[code]
deque <int> fred;
deque <int> fred1;

fred1 = &fred;

fred.push_back(5);
[/code]


i want 2 containers but i want them to point at the same thing

so if i push a number onto fred

fred1 is pointing at the same adrees so , it has the same value

but cant figure out the syntax

fred1 = fred; // would just copy every thing , but i want them to point to the same thing

can any one help.

but after they have been declared though

Share this post


Link to post
Share on other sites
SerialVelocity    166
Create one "real" container which will contain all of your items, then use the reference operator to create more containers that point to the same memory as the real one.

This example should output 5 then 2:
[code]
deque<int> fred;
deque<int> &fredRef;
deque<int> &fredRef2;

// Should output:
// 5
// 2
fredRef.push_back( 5 );
cout << fred.front() << endl;
fredRef2.clear();
fredRef2.push_back( 2 );
cout << fred.front() << endl;
[/code]

Share this post


Link to post
Share on other sites
AndyEsser    394
[quote name='SerialVelocity' timestamp='1306145851' post='4814529']
[code]
deque<int> fred;
deque<int> &fredRef;
deque<int> &fredRef2;

// Should output:
// 5
// 2
fredRef.push_back( 5 );
cout << fred.front() << endl;
fredRef2.clear();
fredRef2.push_back( 2 );
cout << fred.front() << endl;
[/code]
[/quote]

Shouldn't that be:

[code]
deque<int> fred;
deque<int> &fredRef = &fred;
deque<int> &fredRef2 = &fred;

// Should output:
// 5
// 2
fredRef.push_back( 5 );
cout << fred.front() << endl;
fredRef2.clear();
fredRef2.push_back( 2 );
cout << fred.front() << endl;
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this