Jump to content
  • Advertisement
Sign in to follow this  
thedodgeruk

passing stl , by reference?

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

deque <int> fred;
deque <int> fred1;

fred1 = &fred;

fred.push_back(5);



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
Advertisement
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:

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;

Share this post


Link to post
Share on other sites


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;



Shouldn't that be:


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;

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!