Is this passing arguments by reference

P

pkirk25

// get the item, profession and its mats
int get_bill_of_materials(map<string, string> &mats_map);


map<string, string> crafted_items;
int z = get_bill_of_materials(crafted_items);

Have I passed by reference or so I need to do this?

map<string, string> crafted_items;
map<string, string> &crafted_reference = crafted_items;
int z = get_bill_of_materials(crafted_reference);

Many thanks.
 
A

Alf P. Steinbach

* pkirk25:
// get the item, profession and its mats
int get_bill_of_materials(map<string, string> &mats_map);

Presumably the following is meant to illustrate usage:

map<string, string> crafted_items;
int z = get_bill_of_materials(crafted_items);
OK.


Have I passed by reference
Yes.


or so I need to do this?

map<string, string> crafted_items;
map<string, string> &crafted_reference = crafted_items;
int z = get_bill_of_materials(crafted_reference);

No.
 
V

Victor Bazarov

pkirk25 said:
// get the item, profession and its mats
int get_bill_of_materials(map<string, string> &mats_map);


map<string, string> crafted_items;
int z = get_bill_of_materials(crafted_items);

Have I passed by reference

You have.
or so I need to do this?

map<string, string> crafted_items;
map<string, string> &crafted_reference = crafted_items;
int z = get_bill_of_materials(crafted_reference);

Those two pieces are equivalent, except the second one litters
the scope with an unnecessary variable (unless you intend to
use it later somehow).

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top