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
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?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Pointers, yes someone told me about vectors being generalized as pointers.
btw
So is this a reason people use pointers?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. :<
Last edited by bobbelPoP; 08-01-2008 at 12:33 AM.
Yes, though in C++ we would often pass a reference instead of a pointer in such a case.So is this a reason people use pointers?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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.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.So lets, you get a massive amount of data from a file, would you put that into pointer?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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".Are references strict to classes/structs?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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?