0

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.

#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];
}

it's output got as:

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,


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

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by Fbody
0

I've read the book as well. It tends to cover a lot in a hurry, sometimes without adequately explaining the underlying theory. A reference is like a pointer, it is not as powerful, but can be both very useful and very dangerous at the same time, which this code demonstrates.

This statement on Line 24:

string& rStr = refToElement(inventory, 1);

creates a string reference called rStr and establishes a permanent link between rStr and inventory[1].

Later in your code, on Line 36, you have this:

rStr = "Healing Potion";

This statement uses the link between rStr and inventory[1] to modify the value stored in inventory[1] using an assignment statement.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.