< Previous | Contents | Next >

Assigning a Returned Pointer to a Pointer

Next I assign a returned pointer to another pointer with the following line.

string* pStr = ptrToElement(&inventory, 1);

The call to prtToElement() returns a pointer to inventory[1]. The statement assigns that pointer to pStr. This is an efficient assignment because assigning a pointer to a pointer does not involve copying the string object.

To help you understand the results of this line of code, look at Figure 7.5, which shows a representation of pStr after the assignment. (Note that the figure is abstract because the vector inventory doesnt contain the string literals "sword", "armor", and "shield"; instead, it contains string objects.)


image

Figure 7.5

pStr points to the element at position 1 of inventory.


Next I send *pStr to cout and armor is displayed.