Good question, the answer will not become apparent until you get a little further in C++. What is a pointer? A pointer is some that points to an address of where a datatype is stored. Take the following code for example.
Code:
int main( void )
{
int nNum = 10; // Initialize nNum to 10 from Stack
// Initialize a pointer to an int , and set to NULL
int *pNum = NULL;
// Set what pNum points to as the address of nNum
pNum = &nNum;
// If we want the address of nNum
cout << "Address = " << pNum << endl;
// If we dereference the pointer, we get the data
cout << "Data = " << *pNum << endl;
return ( 0 );
}
Alas, I'm probably not the best person to explain this, but say you have a very large Class. Whenever you want a function to manipulate or use data from your class you must pass it as a paramter. Say we have Enemy as a class.
void Foo( int nSomething, Enemy Temp );
This would make a copy of the WHOLE class and pass it to the function. This is bad on numerous levels. Lots of calls to constructors and such and just very slow. We would like to just give the function the address of where the class is and let it manipulate the ACTUAL data instead of a copy.
void Foo( int nSomething, Enemy &Temp );
That's basically one reason pointers are good. Another is you have an array and won't know until run-time how many elements you will need. Consider the following code.
Code:
void Foo( int nItems )
{
char Array[ nItems ]; // This will NOT compile!!
return;
}
void Foo( int nItems )
{
char *pArray = new char [ nItems ]; // This works
. // Something
. // Something
. // Something
delete [] pArray; // Make sure you always free it
return;
}
These might not make that much sense to you now, but they should become clearer as time passes. Hope I didn't mess anything up while explaining. Perhaps someone else can explain it more clearly.