What is the use of having destructor as private?
2017-04-26 16:26
369 查看
Ref http://stackoverflow.com/questions/631783/what-is-the-use-of-having-destructor-as-private
Basically, any time you want some other class to be responsible for the life cycle of your class' objects, or you have reason to prevent the destruction of an object, you can make the destructor private.
For instance, if you're doing some sort of reference counting thing, you can have the object (or manager that has been "friend"ed) responsible for counting the number of references to itself and delete it when the number hits zero. A private dtor would prevent
anybody else from deleting it when there were still references to it.
For another instance, what if you have an object that has a manager (or itself) that may destroy it or may decline to destroy it depending on other conditions in the program, such as a database connection being open or a file being written. You could have a
"request_delete" method in the class or the manager that will check that condition and it will either delete or decline, and return a status telling you what it did. That's far more flexible that just calling "delete".
Basically, any time you want some other class to be responsible for the life cycle of your class' objects, or you have reason to prevent the destruction of an object, you can make the destructor private.
For instance, if you're doing some sort of reference counting thing, you can have the object (or manager that has been "friend"ed) responsible for counting the number of references to itself and delete it when the number hits zero. A private dtor would prevent
anybody else from deleting it when there were still references to it.
For another instance, what if you have an object that has a manager (or itself) that may destroy it or may decline to destroy it depending on other conditions in the program, such as a database connection being open or a file being written. You could have a
"request_delete" method in the class or the manager that will check that condition and it will either delete or decline, and return a status telling you what it did. That's far more flexible that just calling "delete".
相关文章推荐
- What is the general use of Awake(), Start(), Update(), FixedUpdate(), LateUpdate()?
- What is the use of the res/values/public.xml file on Android?
- Question 30: What is the order of destructor calls for an object of class Y inherited from class X that has an object of class A
- what is the use of truncate command
- What is the use of "?
- What is the use of extern in C(C语言中extern的用途)?
- Part 68 - What is the use of NonAction attribute in mvc
- What Is the Purpose of This Book?
- What is the maximum length of a URL
- What is the location of the directory of C header files that match your running kernel?
- Question #49: What is the output of the program?
- what is the mean of "GPRINT Type" in cacti
- What is the location of the directory of C header files that match your running kernel?
- What do you think is the role of parents in preventing crime amongst young people?
- what is the purpose of __ASSEMBLY__?
- What is the maximum length of a URL?
- (Page 1 of 3 )A walking tour of JavaBeans What JavaBeans is, how it works, and why you want to use it
- What is the key of Data Assimilation?
- What is the working set, commit, and private working set memory size?
- Problem 16 - What is the sum of the digits of the number 2^1000?