Just wanted to consider a design question. If you have a C ++ class than other objects, would you use smart pointers to achieve it?
class example {public: // ... private: boost :: scoped_ptr & lt; Proprietary & gt; Information; };
The 'ownership' object can not be stored with the value because it can change in the lifetime of the object.
My view is that on one hand, you make it clear that the object is owned and its deletion is ensured, but on Flipside, you can easily make a regular indicator and remove it. . Is this redundant?
Follow: Just wanted to say thank you for all your replies Thanks to the head-up about Auto_ptr, when the second object is left with a tap pointer When the whole object is copied, I have used auto_ptr on a large scale, but have not thought about it yet. I basically promote all my classes: unless I have good reason, noncopyable promotes so, so there is nothing to worry about, and thanks to the information about memory leaks in exceptions, this It is also good to know. I try not to write things which can cause exceptions in the constructor - there are better ways to do this - so that there is no problem.
I just had one more question, though. The question I wanted was when I asked this question whether someone has actually done this, and you mention all that it is theoretically a good idea, but no one says that they actually do this . I wonder who Surely one object is another indicator with another, it is not a new idea, I used to expect that all of you had done this some time ago. What is happening?
It's a good idea to help ease your code and ensures that When you change the object owned during the lifetime of the object, the last one is destroyed properly.
You should remember that scoped_ptr is noncopyable, however, as long as you do not add your own copy constructor etc., your class becomes non-collacable. (Of course, in the case of raw pointers, nobody will be using the default copy constructor!)
If your class has more than one indicator field, then using scoped_ptr is actually an exception exception Improves:
class C {ownership * o1; Ownership * O2; Public: C (): O1 (new ownership), O2 (new ownership) {} ~ C () {delete O1; O2;}}; Now imagine, during the creation of C, the second "new ownership" throws an exception (for example, out of memory). O1 will be leaked, because C: ~ C (District) will not be called because the object has not been created completely yet. Any fully-formed member of the field, does destructor is called. Therefore, O1 will be properly deleted using a scoped_ptr instead of a plain indicator.
Comments
Post a Comment