Should I store entire objects, or pointers to objects in containers?

后端 未结 10 2087
忘掉有多难
忘掉有多难 2020-11-27 09:31

Designing a new system from scratch. I\'ll be using the STL to store lists and maps of certain long-live objects.

Question: Should I ensure my objects have copy co

10条回答
  •  醉酒成梦
    2020-11-27 09:55

    Sorry to jump in 3 years after the event, but a cautionary note here...

    On my last big project, my central data structure was a set of fairly straightforward objects. About a year into the project, as the requirements evolved, I realised that the object actually needed to be polymorphic. It took a few weeks of difficult and nasty brain surgery to fix the data structure to be a set of base class pointers, and to handle all the collateral damage in object storage, casting, and so on. It took me a couple of months to convince myself that the new code was working. Incidentally, this made me think hard about how well-designed C++'s object model is.

    On my current big project, my central data structure is a set of fairly straightforward objects. About a year into the project (which happens to be today), I realised that the object actually needs to be polymorphic. Back to the net, found this thread, and found Nick's link to the the Boost pointer container library. This is exactly what I had to write last time to fix everything, so I'll give it a go this time around.

    The moral, for me, anyway: if your spec isn't 100% cast in stone, go for pointers, and you may potentially save yourself a lot of work later.

提交回复
热议问题