Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>The Easy way</strong></p> <p>@cos, For the example you have shown, i would say the easiest way to create this system in C++ would be to not trouble with the reference counting. All you have to do would be to make sure that the program flow first destroys the objects (views) which holds the direct references to the objects (paragraphs) in the collection, before the root Document get destroyed. </p> <p><strong>The Tough Way</strong></p> <p>However if you still want to control the lifetimes by reference tracking, you might have to hold references deeper into the hierarchy such that Paragraph objects holds reverse references to the root Document object such that, only when the last paragraph object gets destroyed will the Document object get destructed. </p> <p>Additionally the paragraph references when used inside the Views class and when passed to other classes, would also have to passed around as reference counted interfaces.</p> <p><strong>Toughness</strong></p> <p>This is too much overhead, compared to the simple scheme i listed in the beginning. It avoids all kinds of object counting overheads and more importantly someone who inherits your program does not get trapped in the reference dependency threads traps that criss cross your system. </p> <p><strong>Alternative Platforms</strong></p> <p>This kind-of tooling might be easier to perform in a platform that supports and promotes this style of programming like .NET or Java. </p> <p><strong>You still have to worry about memory</strong></p> <p>Even with a platform such as this you would still have to ensure your objects get de-referenced in a proper manner. Else outstanding references could eat up your memory in the blink of an eye. So you see, reference counting is not the panacea to good programming practices, though it helps avoid lots of error checks and cleanups, which when applied the whole system considerably eases the programmers task.</p> <p><strong>Recommendation</strong></p> <p>That said, coming back to your original question which gave raise to all the reference counting doubts - Is it ok to expose your objects directly from the collection? </p> <p>Programs cannot exist where all classes / all parts of the program are truly interdependent of each other. No, that would be impossible, as a program is the running manifestation of how your classes / modules interact. The ideal design can only minimize the dependencies and not remove them totally. </p> <p>So my opinion would be, <em>yes it is not a bad practice to expose the references to the objects from your collection, to other objects that need to work with them, provided you do this in a sane manner</em></p> <ol> <li><p>Ensure that only a few classes / parts of your program can get such references to ensure minimum interdependency.</p></li> <li><p>Ensure that the references / pointers passed are interfaces and not concrete objects so that the interdependency is avoided between concrete classes. </p></li> <li><p>Ensure that the references are not further passed along deeper into the program. </p></li> <li><p>Ensure that the program logic takes care of destroying the dependent objects, before cleaning up the actual objects that satisfy those references. </p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload