I don't know when to use them. :X I know what they are and everything, but I don't know exactly WHEN to use them, when to use it with a variable/function. etc
Printable View
I don't know when to use them. :X I know what they are and everything, but I don't know exactly WHEN to use them, when to use it with a variable/function. etc
First, you have read the cprogramming tutorial on pointers, right?
Also, you have used say, std::vector before, correct? Have you considered how std::vector might be implemented?
Pointers, yes someone told me about vectors being generalized as pointers. :D
btw
So is this a reason people use pointers?Quote:
If you happen to have a huge piece of data that you want to pass into a function, it's a lot easier to pass its location to the function than to copy every element of the data!
Sorry if I made a few threads back about pointers, it's just a confusing topic at some parts. :<
Yes, though in C++ we would often pass a reference instead of a pointer in such a case.Quote:
So is this a reason people use pointers?
See references as a simplified syntax for pointers.
The only differences are
- A reference is always bound to a variable - it cannot be NULL.
- A reference cannot be rebound. Once it's bound, it can never be changed.
- References do not require * to dereference.
Basically, if a function requires something by reference, it means that argument isn't optional, as opposed to pointers where you can always pass NULL.
Pretty much, yes. A reference must always be bound to something, so just like const, it must be initialized.
So lets, you get a massive amount of data from a file, would you put that into pointer? Or would the file system do that for you and you just have to reference it????
I suggest thinking of references as aliases instead. A reference is another name for an existing object. Consequently, it must refer to an existing object, unlike a pointer. Pointer syntax does not come into the picture, simply because a reference is not a pointer.Quote:
See references as a simplified syntax for pointers.
You cannot put anything other than an address or zero into a pointer. You could put the data into memory, and then use pointers to access the memory.Quote:
So lets, you get a massive amount of data from a file, would you put that into pointer?
It really depends on how where/how you get/store the data.
Is it a typical raw buffer? Then a pointer might be a good choice so you can use it as an array.
If you store it in a vector or stuff, then it's better to pass via reference.
Ah, that's another good one. An alias it is!
No, references can be used on any type. Just append & after it.
BYTE&
int&
MyClass&
std::vector&
etc
No. In this context, when I say object, I mean "a contiguous region of memory holding a value of some type" or "a named or unnamed variable of some type".Quote:
Are references strict to classes/structs?
Another example is, what if you had a game physics engine, and you were trying to define skeleton bone structures ( :P ) for ragdolls, would you store any of that into a pointer?