Do you agree with this statement?

I am reading a c++ book (Thinking In C++ 2nd Edition). I got to a chapter where it talks about STL containers and it states:

Even though the STL containers
hold objects by value (that is, they hold the whole object inside themselves) that’s probably
not the way you’ll generally use them if you’re doing object-oriented programming. That’s
because in OOP, most of the time you’ll create objects on the heap with new and then upcast
the address to the base-class type, later manipulating it as a pointer to the base class. The
beauty of this is that you don’t worry about the specific type of object you’re dealing with,
which greatly reduces the complexity of your code and increases the maintainability of your
program. This process of upcasting is what you try to do in OOP with polymorphism, so
you’ll usually be using containers of pointers.


I have read in many places where it says to avoid using the new keyword to allocate memory.

I have created programs that use containers to hold objects and it never crossed my mind to use the new keyword when adding a object to the container.

So this got me confused. Is the book correct?

One of the better uses of polymorphism is to be able to iterate over a container of base-class pointers and invoke the polymorphic methods on them.

This does not require the use of "new" and "delete". "Thinking in C++" is from either the late '90s or earlier 2000s. C++ was only first officially standardized in 1998. A lot has changed since this. The most major thing is that smart pointers have been standardized. Use a container of unique_ptrs if you need to iterate over polymorphic objects. Use std::make_unique instead of writing "new".

It sounds like you haven't had a need for polymorphism yet. If you don't need to use it, then don't. Using simple containers of objects is fine for a lot of applications. Polymorphism is just a tool like anything else. But polymorphism is really important to classic OOP-style code, which is what the author is talking about. Personally, I think OOP really shines for things like GUI code (see libraries like wxWidgets, Qt, etc.)

C++ is a multi-paradigm language; you can avoid OOP or embrace it. Or use it in combination with other styles.
Last edited on
"Thinking in C++, Second Edition, Volume 1" was published in 2000, "Thinking in C++, Second Edition, Volume 2" in 2003.

Both are very outdated. No C++11 (or later), which radically changed the language for the better.
Last edited on
So this got me confused. Is the book correct?

When the books were written, 1st and 2nd volumes, that information was not wrong.

Since 2003 there have been extensive changes to C++. The books are not relevant any more.

At the time IIRC the STL was not an official part of C++. Since the books were published a lot of the STL was officially incorporated into the C++ standard library.

The Boost libraries have that happen as well. Some feature appears in Boost and a later C++ standard adopts the idea.
Last edited on
@Ganado
It sounds like you haven't had a need for polymorphism yet.

Yes you are right, I have tried to come up with a program that needs polymorphism or incorporate polymorphism in the programs I have created but I haven't been able to do so so far.
One thing I have noticed about me is that I only fully understand (or often find it interesting/relevant) a specific topic/feature when I am able to see a pratical use for it.

@Furry Guy
Since 2003 there have been extensive changes to C++. The books are not relevant any more

By 'not relevant any more' you are suggesting that I should stop reading it. Or not read any book on c++ published prior to C++ 11??
Last edited on
At the time IIRC the STL was not an official part of C++.

The STL was always a part of standard C++ (C++98 and later), so since the books were published after C++ was standardized I the STL was part of the standard and look at this snippet from a link to the book on Amazon:

In Thinking in C++, Volume 2 , the authors cover the finer points of exception handling, defensive programming and string and stream processing that every C++ programmer needs to know. Special attention is given to generic programming where the authors reveal little known techniques for effectively using the Standard Template Library.


But I do agree that this book is outdated, and the OP may be better off finding a newer book.

Topic archived. No new replies allowed.