Ok so I'm currently learning about pointers I have know the basics of them.
1 2 3 4 5 6
int x = 5;
int *p_x = &x;
cout << p_x; //This will be a memory address
cout << x; //This will be 5
cout << *p_x; //This will also be 5
So why do I need pointers? When will I use them? (Not trying to say 'hur dur these are useless why am I learning them'. I'm just trying to figure out where and when I will use them so I can understand them a bit better)
In the future, when you'll need to allocate memory, they'll have to be pointers. Read about memory allocation.
Without memory allocation the variables and such are on the stack, the stack is quite small, if you want a variable that lasts for a long time you will have to declare it on the heap, using memory allocation, which requires pointers.
You use pointers to point.
That would allow you to form relationships between objects.
By instance
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class Car{
//...
Person *driver;
void crash(){
driver->die();
}
};
Person Alice, Bob;
Car racecar;
racecar.set_owner(&Alice);
racecar.set_driver(&Bob);
racecer.crash(); //should kill Bob