1

I came across this question and answer on the software engineering stack exchange earlier. The question is, "Why are pointers not recommended when coding with c++?". The top rated answer offers two points, the second being the one of interest. It states:

Most uses of pointers in C++ are unnecessary.

His specification is somewhat lengthy, but in it the author states, "...modern C++ idioms often don’t need pointers at all.".

The first thing this brings to mind is polymorphism in STL containers. How is this, among many other things in C++ possible without the use of pointers?


After some research, this is all I was able to uncover (spoiler: it wasn't helpful).

AldenB
  • 133
  • 7
  • 3
    Its says "most uses", not "all uses". – Bo Persson Nov 17 '17 at 02:10
  • 1
    `std::vector>`... – Kerrek SB Nov 17 '17 at 02:12
  • @KerrekSB This is just a pointer in disguise. Pointers are still being used. – AldenB Nov 17 '17 at 02:13
  • Why do you say the second question you linked to wasn't helpful? It seems to answer your question exactly. – Benjamin Lindley Nov 17 '17 at 02:16
  • @BenjaminLindley Yes, but could casting using dynamic_case() really be preferable to using pointers? Bjarne Stroustrup even says, "Casts are best avoided. In most cases, consider their use a sign of poor programming.". It seems like general knowledge that casting should be avoided when necessary. Certainly, it isn't necessary in this case. – AldenB Nov 17 '17 at 02:23
  • @BenjaminLindley It did answer the question posted in the title, but the title was malformed. I changed it to better represent my intention. – AldenB Nov 17 '17 at 02:25
  • @AldenB: All of C++ "is just complexity in disguise". But the disguise is valuable. – Kerrek SB Nov 17 '17 at 02:26
  • IMO dynamic polymorphism can be overused. The actual need for it is fairly limited. – Galik Nov 17 '17 at 02:41
  • Not even Rust pretends to not need pointers, so to say that they are "unnecessary" in C++ is laughable. – mnistic Nov 17 '17 at 03:11
  • @mnistic No one is saying they are unnecessary. But they are needed a lot less than some people think. – Galik Nov 17 '17 at 03:15
  • After release of c++11, the recommend way would usually be smart pointer & static polymorphism. If you do want to use dynamic polymorphism and store objects with same base type in the STL containers, raw pointers are preferable. – qduyang Nov 17 '17 at 03:27
  • @Galik somebody is saying it. It's in the quote and in the title of the question. Maybe they were trying to get a different kind of a point across, but using incorrect words incorrectly does not help anyone. – mnistic Nov 17 '17 at 15:07
  • @mnistic The quote says "Most uses..." which is not the same. – Galik Nov 17 '17 at 15:23
  • I'm interested, could you point me to a sufficiently large (>100,000 SLOC) code base on github where pointers are "mostly" absent, let's define "most" as 90% of the code. – mnistic Nov 17 '17 at 15:28

1 Answers1

0

It is possible, at least in C++11, but IMO, a bad idea:

#include <vector>
#include <iostream>
#include <functional>

namespace
{
struct A
{
  virtual void operator()(void) { std::cout << "in A" << std::endl; }
};

struct B : public A
{
  virtual void operator()(void) { std::cout << "in B" << std::endl; }
};
}

int main(void)
{
  A a;
  B b;
  std::vector<std::reference_wrapper<A>> v;
  v.push_back(a);
  v.push_back(b);
  v[0]();
  v[1]();
  return 0;
}

I agree with the comments above. Use smart pointers when you need to do dynamic polymorphism in a container. They work well. Pointers are not bad when they are wrapped in smart pointers. WARNING: in the above, you MUST ensure that a and b exist at least as long as that vector, else the references become undefined (the above code is bad).