3

So I know that on C pointers can do pretty neat stuff. But C++ is object oriented. I can refer to an object instead of using pointers. Am I right? So why having pointers in C++? I can understand that pointers might be implemented for compatibility reasons. Pointers were the power of C. Okay. But really, is there something you can do in C++ and the only (or best) way is using pointers?

Can you give me a good example?

To make it more clear: Is there something in C++ that I can be done only by using pointer? Can I avoid pointers and still do everything someone who uses pointers can do in C++?

George Eco
  • 466
  • 3
  • 16
  • see this:http://stackoverflow.com/questions/162941/why-use-pointers – Suchit kumar Dec 11 '14 at 11:56
  • 1
    @ChrisDrew Polymorphism also works through references. But of course, references are sort of like a restricted form of pointers, and in practice, polymorphism will usually entail dynamic allocation, which in turn requires pointers. – James Kanze Dec 11 '14 at 11:59
  • 1
    Make the lifetime of an object independent of the scope in which it was instantiated. – Grimm The Opiner Dec 11 '14 at 12:01
  • I think that a linked-list is a pretty simple example of something that you can't implement without pointers. – eerorika Dec 11 '14 at 12:01
  • 2
    ...Also - leak memory. ;-) – Grimm The Opiner Dec 11 '14 at 12:01
  • Added this to the main body and also copy pasting here: To make it more clear: Is there something in C++ that I can be done only by using pointer? Can I avoid pointers and still do everything someone who uses pointers can do in C++? – George Eco Dec 11 '14 at 12:12
  • 1
    @GrimmTheOpiner You don't need pointers to leak memory; in fact, you need pointers to not leak memory, since `delete` requires a pointer argument. (But of course, `this` is a pointer, so anytime you have a member function, you have pointers.) – James Kanze Dec 11 '14 at 12:18
  • @JamesKanze can you give an example of memory leaking program without using pointers or constructs returning pointers? – Erbureth Dec 11 '14 at 12:21
  • Grim that you said about instance lifetime is also interesting. – George Eco Dec 11 '14 at 12:21
  • @Erbureth Obviously, you need a `new`. But if you don't assign the results to a pointer, it may leak. (Of course, the constructor may assign `this` to a pointer elsewhere, so that it doesn't leak.) Pointers don't cause memory leaks, dynamic allocation can. – James Kanze Dec 11 '14 at 13:09
  • @Ebureth Well, if you wrote exactly this line of code `new int(0);` then arguably it's a "leak without pointers" as the return from new (a pointer) is never stored. – Grimm The Opiner Dec 11 '14 at 15:15
  • 1
    @GeorgeEco it was going to form part of an answer, but the question is locked out. :-( – Grimm The Opiner Dec 11 '14 at 15:15

2 Answers2

3

For starters, polymorphism only works when accessing through pointers or references. More generally, any sort of a dynamic structure (graphs, etc.) will require pointers, and it's almost always necessary to be able to navagate between objects, which also requires pointers. In fact, the desire to use OO techniques is often the main reason behind a lot of pointers in C++.

You might note that “pure” OO languages generally require that everything be a pointer, for exactly these reasons.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • I mean is there any other way to use a graph or will you need a pointer for sure? If graphs for instance MUST use pointers, your answer solves my question. – George Eco Dec 11 '14 at 12:09
  • Anyway, thanks for giving me a guideline to search further. I accept this as the best answer. – George Eco Dec 11 '14 at 12:13
  • 1
    @GeorgeEco I can't think of any way of implementing a graph without pointers. I can't think of any way of navigating between objects without pointers either. (Of course, you can view the navigation links as edges in a graph.) – James Kanze Dec 11 '14 at 12:16
  • Case solved. I was looking for a good example on this topic and here you are giving me the answer! Thank you James! – George Eco Dec 11 '14 at 12:17
  • 1
    @JamesKanze: You can implement dynamic data structures as elements in an array. Just replace the pointers with indexes. – Blastfurnace Dec 11 '14 at 15:46
  • @Blastfurnace OK. In that sense, you don't need pointers; just put all your data into an array, and use indexes. (In some sense, that's what happens at the hardware level anyway, at least most of the time. Your memory is just one big array, and a pointer is an index into it.) – James Kanze Dec 11 '14 at 15:58
1

The question is a bit broad and philosophical. In fact, it is impossible to completely omit pointers, as usually the used API demands it (e.g. the arguments of the main function). If pointers are not necessary for a specific fixed technical reason, theoretically it would not be necessary to use pointers.

However, this statement is to be taken cum grano salis; if no pointers are used, it is impossible to have dynamic memory management, allocation and deallocation. On the other hand, if usage of the STL is permitted, all of this is nicely encapsulated; pointers would not be involved except for the used APIs, inside the STL and in the definition of the main function.

Codor
  • 17,447
  • 9
  • 29
  • 56