The code below is what you originally had:
1 2 3 4 5 6
|
CBlock Head(400, 200); //created an object in this functions scope
CBlock Body(200, 100); //created an object in this functions scope
CBlock::BlockList.push_back(&Head); //copied the objects address
CBlock::BlockList.push_back(&Body); //copied the objects address
| |
This wouldn't work, because as soon as you left the scope of "CApp.OnInit()", both CBlock Head and Body would be discarded, local variables in class_methods and functions cease to exist after leaving scope.
Only leaving you with reference to non-allocated memory blocks.
In mines:
1 2 3 4 5
|
CBlock Head(100,100); //created an object in this functions scope
//PrevParent = &Head; //Edit: My mistake, Head no longer persists
BlockList.push_back(Head); //copy the object to the end of vector
revParent = &BlockList[BlockList.size]; //give the address of vectors last element
| |
Cbock Head would still be local, therefore not persisting after leaving the method - dead blocks.
BlockList copies Head and pushes it to the last element - BlockList vector persists outside of this method's scope, giving life and taking ownership of new objects with copied parameters of Head.
My original revParent wouldn't work as well, you'd want to give it the address of the vector's copy, not the address of this local Head.
//Edited
revParent receives the an address that will still persist after leaving the scope of the OnInit method.
As for not using the &(address/reference) sign before Head, I've simply decided that I was going to let my vector contain and handle real Objects, and not just pointers to those objects.
Hence:
|
std::vector<CSnake> CSnake::SnakeList;
| |
It would be similar to asking why you'd want to declare primitives rather than address.
1 2 3
|
int i;
char c;
float f;
| |
Rather than say..
1 2 3
|
int * i;
char * c;
float * f;
| |
You can change every primitive in your code to pointer type and assign them new data, but it isn't necessary. By doing that, your basically adding a buffer between you and access of the data.
Same applies to your vector of object pointers.
Why work with pointers when you can access the data directly?
Should the vector not hold data?
The vector is already pointing to many objects/variables. Why point to a point that points to an object/variable?
----
Side note:
AMD mantle is supposedly giving applications/users direct access to GPU without access the CPU.
Original: (somewhat like pointer - no direct access)
APP->CPU->GPU
Mantle: (address to variables - direct access)
APP->GPU
No flame wars please
----
Unless you must use vector pointers, I would suggest on not using pointers (for the vectors objects that is). Having pointers in Objects that point to persistent data in vectors are okay, as long as someone actually has real and maintained data. Preferably the vector should have the higher priority when compare to object, which is what the vector is doing for you.
You could possibly make do with pointers, by doing... [Edited - Removed]
Nah...
Listen to cire:
|
This looks to be way more complicated than it needs to be.
| |