Sign in to follow this  

passing stl , by reference?

This topic is 2400 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

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
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
[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

This topic is 2400 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.

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