Passing Stacks/Maps through a function?

Is there any way you can pass a map and/or stack through a function.

I'm writing a program for a card game using stacks/maps and I am really stuck trying to figure out if you can even pass them.


Any help?



Thanks in advance.
1
2
3
4
5
6
7
8
9
10
void myfunction(const std::stack<int>& foo)
{
  // use 'foo' here
}

int main()
{
  std::stack<int> yam;
  myfunction( yam );
}


That passes by 'const reference'. 'foo' cannot be modified by that function.

You can also pass by non-const reference (std::stack<int>& foo). If you do that, any changes you make to foo will be visible in 'yam'.

You can also pass by value (std::stack<int> foo). If you do that, foo is a copy of the yam. You can modify it, but changes will not affect yam. Passing by value can be expensive for complex objects like stacks/maps.
Topic archived. No new replies allowed.