I have a piece of code that im not 100% what it means.
string* ptrToElemet(Vector<string>* const pVec, int i)
Is this basically saying create a pointer called prtToElement that points to a string. I am slightly confused with the next part. So its saying create a vector but also a constant string pointer called pVec? and then create an interger called i?
// Inventory Pointer
// Demonstrates returning a pointer
#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
//returns a pointer to a string element
string* ptrToElement(vector<string>* const pVec, int i);
int main()
{
vector<string> inventory;
inventory.push_back("sword");
inventory.push_back("armor");
inventory.push_back("shield");
//displays string object that the returned pointer points to
cout << "Sending the object pointed to by returned pointer to cout:\n";
cout << *(ptrToElement(&inventory, 0)) << "\n\n";
//assigns one pointer to another -- inexpensive assignment
cout << "Assigning the returned pointer to another pointer.\n";
string* pStr = ptrToElement(&inventory, 1);
cout << "Sending the object pointed to by new pointer to cout:\n";
cout << *pStr << "\n\n";
//copies a string object -- expensive assignment
cout << "Assigning object pointed to by pointer to a string object.\n";
string str = *(ptrToElement(&inventory, 2));
cout << "Sending the new string object to cout:\n";
cout << str << "\n\n";
//altering the string object through a returned pointer
cout << "Altering an object through a returned pointer.\n";
*pStr = "Healing Potion";
cout << "Sending the altered object to cout:\n";
cout << inventory[1] << endl;
int x; cin >> x;
return 0;
}
string* ptrToElement(vector<string>* const pVec, int i)
{
//returns address of the string in position i of vector that pVec points to
return &((*pVec)[i]);
}
// Inventory Pointer
// Demonstrates returning a pointer
#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
//returns a pointer to a string element
string* ptrToElement(vector<string>* const pVec, int i);
int main()
{
vector<string> inventory;
inventory.push_back("sword");
inventory.push_back("armor");
inventory.push_back("shield");
//displays string object that the returned pointer points to
cout << "Sending the object pointed to by returned pointer to cout:\n";
cout << *(ptrToElement(&inventory, 0)) << "\n\n";
//assigns one pointer to another -- inexpensive assignment
cout << "Assigning the returned pointer to another pointer.\n";
string* pStr = ptrToElement(&inventory, 1);
cout << "Sending the object pointed to by new pointer to cout:\n";
cout << *pStr << "\n\n";
//copies a string object -- expensive assignment
cout << "Assigning object pointed to by pointer to a string object.\n";
string str = *(ptrToElement(&inventory, 2));
cout << "Sending the new string object to cout:\n";
cout << str << "\n\n";
//altering the string object through a returned pointer
cout << "Altering an object through a returned pointer.\n";
*pStr = "Healing Potion";
cout << "Sending the altered object to cout:\n";
cout << inventory[1] << endl;
int x; cin >> x;
return 0;
}
string* ptrToElement(vector<string>* const pVec, int i)
{
//returns address of the string in position i of vector that pVec points to
return &((*pVec)[i]);
}
// Inventory Pointer
// Demonstrates returning a pointer
#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
//returns a pointer to a string element
string* ptrToElement(vector<string>* const pVec, int i);
int main()
{
vector<string> inventory;
inventory.push_back("sword");
inventory.push_back("armor");
inventory.push_back("shield");
//displays string object that the returned pointer points to
cout << "Sending the object pointed to by returned pointer to cout:\n";
cout << *(ptrToElement(&inventory, 0)) << "\n\n";
// the function call returns a pointer to a string pointing to element
// 0 of the inventory vector. This returned pointer is being dereferenced
// and will thus print out "sword".
//assigns one pointer to another -- inexpensive assignment
cout << "Assigning the returned pointer to another pointer.\n";
string* pStr = ptrToElement(&inventory, 1);
// the function call returns a pointer. The pointer pStr is then assigned
// the return value of the function call. Simply put, pStr is now pointing
// to element 1 of the inventory vector
cout << "Sending the object pointed to by new pointer to cout:\n";
cout << *pStr << "\n\n";
//copies a string object -- expensive assignment
cout << "Assigning object pointed to by pointer to a string object.\n";
string str = *(ptrToElement(&inventory, 2));
// the function returns a pointer, which is then dereferenced to give
// us the string. This string is then assigned to 'str'
cout << "Sending the new string object to cout:\n";
cout << str << "\n\n";
//altering the string object through a returned pointer
cout << "Altering an object through a returned pointer.\n";
*pStr = "Healing Potion";
cout << "Sending the altered object to cout:\n";
cout << inventory[1] << endl;
int x; cin >> x;
return 0;
}
string* ptrToElement(vector<string>* const pVec, int i)
{
//returns address of the string in position i of vector that pVec points to
return &((*pVec)[i]);
}
I've included some (hopefully) helpful comments in the code below.
Ok I think I'm getting it now. Think the wording in the book is not the best.
One thing I am still confused about though is the string*
String* ptrToElement (vector <string>*const piece, into i)
I thought if you had string* something
It would make that "something" a pointer because of the *, but it doesn't in this case, I'm really confused how this is not being declared a pointer.
I have only started to learn c++ so confused easily XD
In this context, the string* is a return type. It cannot possibly be a declaration of a pointer, as ptrToElement is followed by 2 parameters in parenthesis (We know there are parameters as they start with a data type).
Declaration and initialization of a string pointer would be like this: string* pStr = &str
If you still don't get it, think of it in terms of simpler code: int a; // makes an int variable called 'a'
int a = num; // declares an int 'a' and initializes it with 'num'
1 2
int a(int num);
// function header: return type is int, function name is 'a', and it takes a single int //parameter called 'num