Thanks for everyone's help in the past and an advanced thanks.
I just have one QUESTION and some ANALYSIS to make sure I understand this entirely.
1) ANALYSIS:
It just dawned on me that every time I saw something created on the heap, which I believe was always created with a pointer. This is probably because "new" always returns a pointer to the address that was created. So, it is fair to say that anytime you want to create something on the heap (using new/delete), a pointer is REQUIRED, and you cannot create on the heap without a pointer. Which I believe to be correct.
2) ANALYSIS:
Here the "new" creates memory on the heap. The array is a pointer to a pointer of objects, and the array creates copies of the pointer address to the object. Which I believe is correct.
1 2 3 4 5 6
|
//______________________________________________
//2) Car array on heap, method 1
Car* myCar1 = new Car();
Car* myCar2 = new Car();
Car* myCarColl1[ARRAY_SIZE] = {myCar1, myCar2 };
|
Below also creates a pointer to an array, of pointers to objects.
1 2 3 4 5
|
//______________________________________________
//3) Car array on heap, method 2
Car* myCarColl2[ARRAY_SIZE] = { NULL };
myCarColl2[0] = new Car();
myCarColl2[1] = new Car();
|
3) QUESTION:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
//2) Car array on heap, method 1
Car* myCar1 = new Car();
Car* myCar2 = new Car();
Car* myCarColl1[ARRAY_SIZE] = {myCar1, myCar2 };
(*myCarColl1)[0].GetCarID(); //WORKS
myCarColl1[0]->GetCarID(); //WORKS
myCarColl1[1]->GetCarID(); //WORKS
cout << ">>>>>";
(*(myCarColl1 + 1))[0].GetCarID();
//(*myCarColl1)[1].GetCarID(); //DOES NOT WORK
//(*(myCarColl1 + 1))[1].GetCarID(); //DOES NOT WORK
//(*myCarColl1 + 1)[1].GetCarID(); //DOES NOT WORK
//(*(myCarColl1 + 1)).GetCarID(); //DOES NOT WORK
|
Now I am kicking myself on this one.
The, above 3 "//WORKS" as I expected them to and let me just analyze them to make sure I understand why they are working.
(*myCarColl1)[0].GetCarID();
(*myCarColl1) <----That part says the thing that I am pointing to, which is the whole array, and more specifically the address of index [0], which holds the pointer to the address of the object.
[0].GetCarID(); <----This part resolves the index that has a pointer to the object, which is the object itself & then runs the function.
So, why would the below not work?
|
//(*myCarColl1)[1].GetCarID(); //DOES NOT WORK
|
(*myCarColl1) <----The thing that I am pointing to, which is the array at index[0]
[1].GetCarID(); <----Does not resolve the next address value of (object itself).
Not even below works.
|
//(*(myCarColl1 + 1))[1].GetCarID(); //DOES NOT WORK
|
But below works with an index of [0]....what???? Now I am totally mystified???
Is there any other way to write/resolve an array index of pointer to a pointer?
|
(*(myCarColl1 + 1))[0].GetCarID();
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include <iostream>
using namespace std;
class Car
{
public:
static int CarID;
int myCarID = 0;
Car()
{
cout << "Honk, honk!!!\n";
++CarID;
myCarID = CarID;
}
~Car(){}
void GetCarID() const
{
cout << myCarID << endl;
}
};
int Car::CarID = 0;
int main()
{
const int ARRAY_SIZE = 2;
//1) Car array on stack
Car myCar[ARRAY_SIZE] = { Car(), Car() };
myCar[0].GetCarID();
myCar[1].GetCarID();
cout << "\n\n";
//______________________________________________
//2) Car array on heap, method 1
Car* myCar1 = new Car();
Car* myCar2 = new Car();
Car* myCarColl1[ARRAY_SIZE] = {myCar1, myCar2 };
(*myCarColl1)[0].GetCarID();
myCarColl1[0]->GetCarID();
myCarColl1[1]->GetCarID();
cout << ">>>>>";
(*(myCarColl1 + 1))[0].GetCarID();
//(*myCarColl1)[1].GetCarID(); //DOES NOT WORK
//(*(myCarColl1 + 1))[1].GetCarID(); //DOES NOT WORK
//(*myCarColl1 + 1)[1].GetCarID(); //DOES NOT WORK
//(*(myCarColl1 + 1)).GetCarID(); //DOES NOT WORK
delete myCar1;
delete myCar2;
myCar1 = NULL;
myCar2 = NULL;
cout << "\n\n";
//______________________________________________
//3) Car array on heap, method 2
Car* myCarColl2[ARRAY_SIZE] = { NULL };
myCarColl2[0] = new Car();
myCarColl2[1] = new Car();
(*myCarColl2)[0].GetCarID();
myCarColl2[0]->GetCarID();
myCarColl2[1]->GetCarID();
(*(myCarColl2 + 1))[0].GetCarID();
//(*myCarColl2)[1].GetCarID(); //DOES NOT WORK
delete myCarColl2[0];
delete myCarColl2[1];
myCarColl2[0] = NULL;
myCarColl2[1] = NULL;
//delete[] myCar;
cout << "\n\n";
return 0;
}
|