Beginning C++ programmers, or people coming from Java or C# tend to use pointers everywhere.
You need an object of type Foo? Foo* f = new Foo();
. Class Bar
contains a member object? Make that a pointer to a dynamically allocated object.
And that is not the right way to go. The Foo
object can be allocated on the stack. There's no need to use dynamic allocation, and there's no need to use a pointer. Bar
can store the member object directly, rather than a pointer to it.
In good C++ code, pointers should be rare. Not because eliminating pointers in itself makes your code better, but because heavy use of pointers is a sign that the person who wrote the code pretends the language is Java or C#.
Other examples are reliance on arrays and pointers where std::vector
would've solved the problem. Or using pointers to implement pass by reference, when actual references could have been used.