Hello,

I am wondering how do we initialize a vector (or even an array) of pointers when the objects that will eventually be pointed to don't exist yet? I've been searching the internet for a while, but most examples assume that the objects already exist.

I hope this won't be too confusing but to further illustrate what I'm trying to do. Let's say that I want to simulate the passengers on a plane using: vector<Person*> passengers. When I create an instance of the Airplane class that passengers will be a member of, passengers should be empty. Only when I use a method like sellTicket(Person *personPtr) or maybe boardPassenger(Person *personPtr) will a pointer be added to the vector. So how can I initialize passengers so that if I, for example, run a method that lists all the Person objects pointed to by passengers prior to actually adding any pointers I won't crash the program because I'd have some value to evaluate.

Does that make sense to anyone? I hope so =)

Recommended Answers

All 8 Replies

To address your first issue: void pointers. Google it. It's basically a pointer that has no type bound to it. I haven't used them too much but I think they fit your needs.

Your second problem:
You can check to see if there is data to be read. Use the empty() method of the vector class to see if it is empty or not. As for arrays, if you initialize the array of pointers to NULL, rather than having bad pointers. Then you can do

if(passengers[i])
//output data

To address your first issue: void pointers. Google it. It's basically a pointer that has no type bound to it. I haven't used them too much but I think they fit your needs.

Dumb dumb dumb dumb dumb dumb dumb dumb what the hell are you talking about.

klactose, the solution is to start with an empty vector and use push_back, which adds a value to the end of the vector (and increases its size() by one).

Thanks CoolGamer, I'll give those suggestions a try.

Dumb dumb dumb dumb dumb dumb dumb dumb what the hell are you talking about.

klactose, the solution is to start with an empty vector and use push_back, which adds a value to the end of the vector (and increases its size() by one).

Hi Sarehu. I guess that's the crux of my question how would you initialize the vector?

vector<Person *personPtr> passengers = ?????

personPtr?

vector<Person*> passengers;  // initializes to an empty vector.

personPtr?

vector<Person*> passengers;  // initializes to an empty vector.

Yep. Here's an example.

#include <vector>
#include <iostream>
using namespace std;


void DisplayVector(vector <int*> myVector);

int main ()
{
    vector <int*> myVector;
    cout << "Display empty vector" << endl;
    DisplayVector(myVector);



    int* a = new int;
    int* b = new int;
    *a = 8;
    *b = 9;
    myVector.push_back(a);
    myVector.push_back(b);
    cout << "Display non-empty vector" << endl;
    DisplayVector(myVector);
    return 0;
}
    
       
void DisplayVector(vector <int*> myVector)
{
     for (int i = 0; i < myVector.size(); i++)
     {
         int* number = myVector.at(i);
         cout << *number << endl;
     }
}

Reasonable question, evasive answers...

It's operator new creates new objects of Person class. It returns a pointer to a new passenger and you may add it to the vector of pointers. Be careful: the object must exist while passenger vector exists. May be, using vector<Person> (not vector<Person*>) is the best (safe) variant, but you may use vector<Person*> too - for example:

class Airplane
{ ...
  public: void sellTicket(Person* p)
...
  vector<Person*> passengers;
};

void Airplane::sellTicket(Person* p)
{ ...
  passengers.push_back(p);
...
}
...
Airplane plane(...);
...
Person* p = new Person(...);
plane.sellTicket(p);
...

Now you may use the vector, for example:

class Airplane
{...
public: void print() const
{ // or use vector<Person*>::const_iterator (see std::vector docs)
   cout << passengers.size() << " passengers now:\n"; 
   for (int i = 0; i < passengers.size(); ++i)
      cout << i << ".\t" << passengers[i]->name() << "\n";
   cout << "--- end of list ---" << endl;
}
...
Airplane plane(...);
...
plane.print();
...
};

Don't forget to delete Person objects when the plane crashed...

Thank you all for your replies... I suppose I was already doing it correctly because my code is very much like VernonDozier and ArkM's examples

I just wasn't sure if there needed to be an explicit initialization of the vector. But since a declaration like vector<ClassType*> vectorName is enough to serve as an initializer then I will just check if vectorName.size() > 0 to determine whether or not my methods attempt to access it's contents.

Thanks again all!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.