It is clear that pointers are used in C and C++ everywhere, and it’s for lower level access. The fact you can create and access new instances of variables, objects, and so on, in memory run time, gives us an amazing power. For example, let’s say we’re building a multi-user program and will want to have a class to handle each specific user data. Let’s call it “NetworkUser”, and let’s say it will contain for example, user entered nick name. So It does not make sense to create a new array of 20000 “NetworkUsers” because it will be instanced when the program runs and will will consume all memory, and well, plus let’s say we don’t know how many user we will have at once, so in this case the best way is to create a dynamic array of pointers to NetworkUsers and instantiate a new one just every time a new users connects, all dynamically… So to illustrate my idea I tried to build this example that creates a pointer to an array of pointers and so on…
#include <iostream>
#include <string>
using namespace std;
//Class Network User
class NetworkUser {
private:
//declaring a string pointer to to a string that will to contain nickname
string* _nickName;
public:
//constructor - initianilze nickName
NetworkUser(){
//nothing to initialize
}
//descrutor, called by: delete
~NetworkUser(){
//nothing
}
//subroutine to assign a the pointer of nickname to
//the internal class pointer to nickname
void assignNickName(string* nickName){
_nickName = nickName;
}
//function to return the pointer to the nickname string
//of this class/object
string* getNickName(){
return _nickName;
}
};
//man program entry point
int main(int argc, const char * argv[]) {
//declaring a pointer to
//an array of NetworkUser pointers
NetworkUser** arrNetworkUsers = nullptr;
//to count new users
int usersCount = 0;
//temporal pointer to string that will cointain the nick name
string* nickName;
//loop forever
while (true){
//instance a new String class
nickName = new string();
//ask the user to enter a new nick or end to finish
cout << "Type a new nick name or end to finish: ";
//wait for the new nick
cin >> *nickName;
cout << endl;
//if nickname is not empty and not end
if(*nickName!="" && *nickName!="end"){
//create a temporal pointer to
//a new array of NetworkUsers pointers of the lenght of users count
NetworkUser** tmpNetworkUsers = new NetworkUser *[usersCount];
//if usersCount is not 0 then travel the previous
//pointers array and copy the pointer to the new temporal array
for(int i=0; i<usersCount; i++){
tmpNetworkUsers[i] = arrNetworkUsers[i];
}
//reasign the pointer to the pointers array to the new temporal array
//that contains the pointer to the new NetworkUser instance
arrNetworkUsers = tmpNetworkUsers;
//create a new pointer to a new instance of NetworkUser
NetworkUser* tmpNetUser = new NetworkUser();
//add the new user nickname to current instance
tmpNetUser->assignNickName(nickName);
//add the pointer to the pointers array
arrNetworkUsers[usersCount] = tmpNetUser;
//increment users count
usersCount++;
} else {
break; //user wanted to end
}
}
//if there is any user
if(usersCount>0){
//show the count
cout << "\nUsers nick added: " << usersCount << endl;
//travel thru the array of pointers of NetworkUsers instances
//and get the specific user nickname
string* tmpNick;
for(int userIndex=0; userIndex<usersCount; userIndex++){
tmpNick = arrNetworkUsers[userIndex]->getNickName();
cout << "User #" << userIndex << ": " << *tmpNick << endl;
}
cout << endl;
}
//delete each instance of NetworkUsers classes in the pointers array
for(int instanceIndex = 0; instanceIndex < usersCount; instanceIndex++){
delete arrNetworkUsers[instanceIndex];
}
return 0;
}
Not sure if this looks correct, but it worked. I am sure this could be better written and might have problems, I am not good with pointers “yet” hehe, but I think this illustrates my point…