Is this passing arguments by reference

Discussion in 'C++' started by pkirk25, Oct 31, 2006.

  1. pkirk25

    pkirk25 Guest

    // 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.
     
    pkirk25, Oct 31, 2006
    #1
    1. Advertising

  2. * 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.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 31, 2006
    #2
    1. Advertising

  3. pkirk25 wrote:
    > // 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 31, 2006
    #3
  4. pkirk25

    pkirk25 Guest

    That saves a little typing then - many thanks!
     
    pkirk25, Oct 31, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam pal
    Replies:
    3
    Views:
    551
    E. Robert Tisdale
    Jul 16, 2003
  2. Giff
    Replies:
    3
    Views:
    310
  3. Dean
    Replies:
    3
    Views:
    182
  4. Replies:
    2
    Views:
    150
  5. oldyork90
    Replies:
    10
    Views:
    348
    Jorge
    Sep 27, 2008
Loading...

Share This Page