C++ Regarding the value return by Reference...

  • admod
  • Newbie
  • Newbie
  • admod
  • Posts: 8

Post 3+ Months Ago

Hi pals,
I am newbie in C++ Development. I saw a code in a book named "Beginning C++ Game Programming". I have a doubt regarding the Following Working Code.

CPP Code: [ Select ]
#include <iostream>
#include <string>
#include <vector>
 
using namespace std;
 
//returns a reference to a string
string& refToElement(vector<string>& vec, int i);
 
 
int main()
{
    vector<string> inventory;
    inventory.push_back("sword");
    inventory.push_back("armor");
    inventory.push_back("shield");
 
    //displays string that the returned reference refers to
    cout << "Sending the returned reference to cout:\n";
    cout << refToElement(inventory, 0) << "\n\n";
 
    //assigns one reference to another-inexpensive assignment
    cout << "Assigning the returned reference to another reference.\n";
    string& rStr = refToElement(inventory, 1);
    cout << "Sending the new reference to cout:\n";
    cout << rStr << "\n\n";
 
    //copies a string object-expensive assignment
    cout << "Assigning the returned reference to a string object.\n";
    string str = refToElement(inventory, 2);
    cout << "Sending the new string object to cout:\n";
    cout << str << "\n\n";
 
    //altering the string object through a returned reference
    cout << "Altering an object through a returned reference.\n";
    rStr = "Healing Potion";
    cout << "Sending the altered object to cout:\n";
    cout << inventory[1] << endl;
 
    return 0;
}
 
//returns a reference to a string
string& refToElement(vector<string>& vec, int i)
{
    return vec[i];
}
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. //returns a reference to a string
  8. string& refToElement(vector<string>& vec, int i);
  9.  
  10.  
  11. int main()
  12. {
  13.     vector<string> inventory;
  14.     inventory.push_back("sword");
  15.     inventory.push_back("armor");
  16.     inventory.push_back("shield");
  17.  
  18.     //displays string that the returned reference refers to
  19.     cout << "Sending the returned reference to cout:\n";
  20.     cout << refToElement(inventory, 0) << "\n\n";
  21.  
  22.     //assigns one reference to another-inexpensive assignment
  23.     cout << "Assigning the returned reference to another reference.\n";
  24.     string& rStr = refToElement(inventory, 1);
  25.     cout << "Sending the new reference to cout:\n";
  26.     cout << rStr << "\n\n";
  27.  
  28.     //copies a string object-expensive assignment
  29.     cout << "Assigning the returned reference to a string object.\n";
  30.     string str = refToElement(inventory, 2);
  31.     cout << "Sending the new string object to cout:\n";
  32.     cout << str << "\n\n";
  33.  
  34.     //altering the string object through a returned reference
  35.     cout << "Altering an object through a returned reference.\n";
  36.     rStr = "Healing Potion";
  37.     cout << "Sending the altered object to cout:\n";
  38.     cout << inventory[1] << endl;
  39.  
  40.     return 0;
  41. }
  42.  
  43. //returns a reference to a string
  44. string& refToElement(vector<string>& vec, int i)
  45. {
  46.     return vec[i];
  47. }


it's output got as:

Quote:
Sending the returned reference to cout:
sword

Assigning the returned reference to another reference.
Sending the new reference to cout:
armor

Assigning the returned reference to a string object.
Sending the new string object to cout:
shield

Altering an object through a returned reference.
Sending the altered object to cout:
Healing Potion


in that I don't understand How to get the last line of output i.e,


Quote:
Altering an object through a returned reference.
Sending the altered object to cout:
Healing Potion



Do any one can explain this. I am waiting your reply.

Thankfully
Anes P.A
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

admod wrote:
Hi pals,

in that I don't understand How to get the last line of output i.e,


Quote:
Altering an object through a returned reference.
Sending the altered object to cout:
Healing Potion



Do any one can explain this. I am waiting your reply.


The code is Perfect and fine. Here is Explanation.

Look Each Data( Variable ) represents a memory Location( Normally in Primary Memory.)
in This line
Code: [ Select ]
string& rStr = refToElement(inventory, 1);

'rStr' is asked to store the address of memory location where inventory[1] is stored. The function 'refToElement' returns Memory location not the actual value stored at location.

Due to this code
rStr and inventory[1] points to same memory location. So if any change is made to any of them, it is represented by other. Its just like One Room with 2 Doors.

so
Code: [ Select ]
rStr = "Healing Potion";
  cout << "Sending the altered object to cout:\n";
  cout << inventory[1] << endl;
  1. rStr = "Healing Potion";
  2.   cout << "Sending the altered object to cout:\n";
  3.   cout << inventory[1] << endl;


change to 'rStr' is represented by 'inventory1'
  • admod
  • Newbie
  • Newbie
  • admod
  • Posts: 8

Post 3+ Months Ago

Hi Satwant,

Thanks For your Good Reply . I got the Idea , and so I make pending in my C++ study. So I plan
to restart same in this weekend itself. Waiting your further Help and Give my warm regards
to you my GURU.

Thankfully,
Anes P.A
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

You Welcome :)

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 125 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.