Shuttleu
05-31-2009, 10:38 PM
i have been learning C++ recently and i have just read about pointers
this is what i have learnt and i want to know if i have got anything wrong or missed anything out
pointers only hold a memory address
put a & infront of a var name to return a memory address
to access the memory the pointer points to you put * infront of the pointer name
to create a pointer you put the var type it will hold followed by a * then the pointer name
when you create a pointer always assign it a to a var or set it to 0
to create a memory location in the free store use the keyword new followed by the object type or var type that it can hold which will then return a memory location that a pointer can hold
to free the memory in the free store call delete on the pointer
once you have deleted a pointer set it to 0 or NULL or if you call delete on it again it can cause the program to crash
if you assign a location in the free store make sure to free it before assigning it to another location or you will create a memory leak
if you create a pointer that is local and points to the free store, make sure to call delete before the function it was created in ends
to access a function or var inside a a class by using a pointer use one of the following: (*objectpointername).objectfinction or objectpointername->objectunction
to create a pointer which cant be changes use vartype const pointername
to create a pointer which points to a var that cant be changed use const vartype pointername
to create a pointer that cant be changed that points to a var that cant be changed use
const vartype const pointername
~shut
this is what i have learnt and i want to know if i have got anything wrong or missed anything out
pointers only hold a memory address
put a & infront of a var name to return a memory address
to access the memory the pointer points to you put * infront of the pointer name
to create a pointer you put the var type it will hold followed by a * then the pointer name
when you create a pointer always assign it a to a var or set it to 0
to create a memory location in the free store use the keyword new followed by the object type or var type that it can hold which will then return a memory location that a pointer can hold
to free the memory in the free store call delete on the pointer
once you have deleted a pointer set it to 0 or NULL or if you call delete on it again it can cause the program to crash
if you assign a location in the free store make sure to free it before assigning it to another location or you will create a memory leak
if you create a pointer that is local and points to the free store, make sure to call delete before the function it was created in ends
to access a function or var inside a a class by using a pointer use one of the following: (*objectpointername).objectfinction or objectpointername->objectunction
to create a pointer which cant be changes use vartype const pointername
to create a pointer which points to a var that cant be changed use const vartype pointername
to create a pointer that cant be changed that points to a var that cant be changed use
const vartype const pointername
~shut