0
Why are Pointers Useful?
Why are C++ Pointers userful? I pretty much know how they work, and I've seen examples, but I just can't wrap my head around why they're useful? I've seen notes in the lessons about them being faster and more efficient, but why? (Another question)
5 Respuestas
+ 2
Pointers are like variables. so they have specific size. instead of for example copying a big vector between functions you can just take its adress. pointers point to a adress. so for example linked lists need pointers to tell where to iterate. the only thing that needs to be changed in a linked list are the pointers and not the actual nodes. basically it means that pointers can optimize memory usage alot by pointing instead of being. think of a train. you want to rearrange the order of rail cars. so instead of trying to move THEM around you just reconfigure their linking. hope its not too confusing :p
+ 1
np!
0
Pointers are used for:
- Memory management (heap)
- Passing by reference not by value in a function or method
- Data structures
- Probably a lot more
They can be faster when you use them to pass by reference rather than by value. If you didn’t know on default C++ passes function arguments by value meaning the values are copied because of this it takes time to copy the object but you also can’t edit the original value. To skip this overhead (copying) and to gain the ability to edit the value directly you can make the parameter a pointer but be careful when editing the original/real value as it’s easy to mess up and create bugs.
0
TurtleShell thanks (I knew the bit about passing by value tho ;) )
if you wouldn't mind asking, jeres5 another question:
in the examples, it shows a Monster/Ninja being created, and then an Enemy pointer having the address as it's value. to me, it seems pointless (ehhhhh), but the notes says it's faster and more efficient, what makes this more efficient than directly dealing with the instance (rather than the base class pointer)?
0
Coinage thanks! it helped a bit!