Storing a list of arbitrary objects in C++

前端 未结 13 2020
野趣味
野趣味 2021-02-06 08:29

In Java, you can have a List of Objects. You can add objects of multiple types, then retrieve them, check their type, and perform the appropriate action for that type.
For e

13条回答
  •  感情败类
    2021-02-06 09:03

    I am a fairly inexperienced, but here's what I'd go with-

    1. Create a base class for all classes you need to manipulate.
    2. Write container class/ reuse container class. (Revised after seeing other answers -My previous point was too cryptic.)
    3. Write similar code.

    I am sure a much better solution is possible. I am also sure a better explanation is possible. I've learnt that I have some bad C++ programming habits, so I've tried to convey my idea without getting into code.

    I hope this helps.

提交回复
热议问题