Can I have polymorphic containers with value semantics in C++?

后端 未结 9 1439
半阙折子戏
半阙折子戏 2020-12-02 18:35

As a general rule, I prefer using value rather than pointer semantics in C++ (ie using vector instead of vector). Usuall

9条回答
  •  死守一世寂寞
    2020-12-02 19:25

    Take a look at static_cast and reinterpret_cast
    In C++ Programming Language, 3rd ed, Bjarne Stroustrup describes it on page 130. There's a whole section on this in Chapter 6.
    You can recast your Parent class to Child class. This requires you to know when each one is which. In the book, Dr. Stroustrup talks about different techniques to avoid this situation.

    Do not do this. This negates the polymorphism that you're trying to achieve in the first place!

提交回复
热议问题