Is there something you can do that is the only way to use pointers in C ++?

So I know that pointers can do pretty neat things in C. But C ++ is object oriented. I can refer to an object instead of using pointers. I'm right? So why with pointers in C ++? I can understand that pointers can be implemented for compatibility reasons. The pointers were strength C. Okay. But really, is there something you can do in C ++ that only (or better) way uses pointers?

Can you give me a good example?

To make it clearer: Is there something in C ++ that I can only do with a pointer? Can I avoid pointers and still do whatever C ++ can do with pointers?

+3


source to share


2 answers


First, polymorphism only works when accessed via pointers or references. More generally, any kind of dynamic structure (graphics, etc.) will require pointers, and this is almost always necessary to be able to magnetize between objects, which also requires pointers. In fact, the desire to use OO methods is often the main reason for many pointers in C ++.



You will notice that pure OO languages ​​generally require everything to be a pointer for these very reasons.

+3


source


The question is a bit broad and philosophical. It is actually not possible to omit pointers entirely, as the API usually requires (e.g. function arguments main

). If pointers are not needed for a specific fixed technical reason, there is theoretically no need to use pointers.



However, this statement must be accepted cum grano salis; if no pointers are used, it is impossible to have dynamic memory management, allocation and deallocation. On the other hand, if the use of the STL is allowed, the whole thing is nicely encapsulated; pointers will not be involved, except for the APIs used, within the STL and function definitions main

.

+1


source







All Articles