Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Should class subobjects be ptrs to freestore allocated objs, or should I use “composition”?

0
Posted

Should class subobjects be ptrs to freestore allocated objs, or should I use “composition”?

0

Composition. Your member objects should normally be “contained” in the composite object (but not always; “wrapper” objects are a good example of where you want a ptr/ref; also the N-to-1-uses-a relationship needs something like a ptr/ref). There are three reasons why fully contained member objects (“composition”) has better performance than ptrs to freestore-allocated member objects: • Extra layer to indirection every time you need to access the member object. • Extra freestore allocations (“new” in constructor, “delete” in destructor). • Extra dynamic binding (reason given below).

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123