I read from somewhere that when using C++ it is recommended not to use pointers. Why is pointers such a bad idea when you are using C++. For C programmers that are used to using pointers, what is the better alternative and approach in C++?
feedback
|
I think they mean you should use smart pointers instead of regular pointers.
In C++ the emphasis would be on garbage collection and preventing memory leaks (just to name two). Pointers are a fundamental part of the language, so not using them is pretty much impossible except in the most trival of programs. | |||||
feedback
|
Simply because there are abstractions available to your which hide the more temperamental aspects of using pointers, such as access to raw memory and cleaning up after your allocations. With smart pointers, container classes, and design patterns like RAII, the need for using raw pointers is diminished. That said, like any abstraction, you should understand how they actually work before moving beyond them. | |||
feedback
|
One of reasons is too wide application of pointers. They can be used for iteration over containers, for avoiding copying large objects when passing to function, non-trivial life-time management, accessing to random places in memory, etc. And once you used them for one purpose, other their features become available immediately independently on intent. Selection of a tool for exact purpose makes code simpler and intent more visible - iterators for iterations, smart pointers for life-time management, etc. | |||
feedback
|
Beside the risk of memory leaks stated by @jmquigley pointer and pointeraritmetics can be considered problematic because pointers can point everywhere in memory causing "hard to find bugs" and "security vulnerableties". Thats wy they were nearly abendoned in c# and java. | |||
feedback
|
Besides the reasons already listed, there is an obvious one: better optimisations. Aliasing analysis is far too complicated in presense of a pointer arithmetics, whereas references hints an optimiser, so a much deeper aliasing analysis is possible if only references are used. | ||||
feedback
|
You can perfectly use pointers in C++, but you shouldn't if your intend is to use object oriented programming (which is not always the case when you use C++), because pointers let you bypass all the object oriented features, like attribute or method privacy, and so you can use what ever object you want, which is bad in OOP. So if you use pointers, you're not doing OOP anymore. | |||||||||||||
feedback
|
"C++" is "C" plus Objects and Classes. "C" already had pointers and other stuff. Pointer usage is difficult, its very easy to create errors / exceptions, and is difficult to teach and difficult to understand. Many new programming languages pretend not to use pointers with objects, like Java, .NET, Delphi, Vala, PHP, Scala. But, pointers are still used, "behind the scenes". Anyway, I consider "pointer (s)" as a Programming Pattern, as a valid way to solve certain problems, as well as Object Oriented Programming does. Other developers may have a different opinion. But, I suggest students and programmers learn how to: (1) Use pointers without objects (2) objects without pointers (3) explicit pointers to objects (4) "hidden" pointers to objects ("references") ;-) Even if is difficult to teach, and diffcult to learn. Object Pascal (not Delphi) and C++ (not Java or C#) can be used for those goals. | |||||
feedback
|