w**t 发帖数: 592 | 1 1. Why destructor is not virtual by default?
2. How to bring destructor back once the object is out of scope?
3. How to stop 1 people amony several others from acessing a function object
? |
p*u 发帖数: 2454 | 2
There are many cases when you surely don't want your class to be base class
of other classes. By making a dtor non-virtual, a class is not supposed to be
derived from. As for performance, virtual dtors increase the size of
object(pointer to vt). The problem with virtual dtors is not only the
overhead associated with dynamic binding but also because most dtors are
either explicitly or implicitly inlined. virtual dtors cannot be inlined.
don't quite understand the question...
make the operator()
【在 w**t 的大作中提到】 : 1. Why destructor is not virtual by default? : 2. How to bring destructor back once the object is out of scope? : 3. How to stop 1 people amony several others from acessing a function object : ?
|
w**t 发帖数: 592 | 3 don't quite understand the question...
I didnt get it either. Question: when the destructor is called? Answer: when
the object is out of scope? Then he asked how to bring it back (call the
destructor??) even if the object is out of scope? |
S*****n 发帖数: 227 | 4 Did u miss some contexts for these questions? or it looks quite weird.
object
【在 w**t 的大作中提到】 : 1. Why destructor is not virtual by default? : 2. How to bring destructor back once the object is out of scope? : 3. How to stop 1 people amony several others from acessing a function object : ?
|
p*u 发帖数: 2454 | 5
when
~~~~~~~~~~~~~~~~~~~~~~~~~~~or when delete gets called
~~~~~~~~~~~~~~~I guess he meant how to bring the object back when it's out
of scope. just declare the object as static.
【在 w**t 的大作中提到】 : don't quite understand the question... : I didnt get it either. Question: when the destructor is called? Answer: when : the object is out of scope? Then he asked how to bring it back (call the : destructor??) even if the object is out of scope?
|
k*k 发帖数: 508 | 6 I think he is asking "how to bring the object back"
in C#, you can make a WeakReference to the object. The object is still
garbage collectable. When you try to retrieve the object back via the
weak reference, if the object hasn't been collected, you get it alive
and have a reference (a strong reference) to it; if the object has been
collected, I guess there's no way to get it back.
when
【在 w**t 的大作中提到】 : don't quite understand the question... : I didnt get it either. Question: when the destructor is called? Answer: when : the object is out of scope? Then he asked how to bring it back (call the : destructor??) even if the object is out of scope?
|
p*u 发帖数: 2454 | 7 most of the questions are vague. In practice it's barely somebody will try
to beat GC to an out-of-scope object, it's weird.
【在 k*k 的大作中提到】 : I think he is asking "how to bring the object back" : in C#, you can make a WeakReference to the object. The object is still : garbage collectable. When you try to retrieve the object back via the : weak reference, if the object hasn't been collected, you get it alive : and have a reference (a strong reference) to it; if the object has been : collected, I guess there's no way to get it back. : : when
|
k*k 发帖数: 508 | 8 actually it's not weird to get the out-of-scope object. It's a way to
boost performance. Yes, you can always use a global scope variable to
maintain the reference, but if the object is pretty large and you are
not sure whether you will use it in the future, weakreference is very
useful. say, you have a big array of 1GB elements, you need to read
the content from disk I/O. If you need to do some operation on this
array and get some result several times but don't always need it
there, you can main
【在 p*u 的大作中提到】 : most of the questions are vague. In practice it's barely somebody will try : to beat GC to an out-of-scope object, it's weird.
|
p*u 发帖数: 2454 | 9
~~~~~~~~~~~~~~who will put it on stack???
【在 k*k 的大作中提到】 : actually it's not weird to get the out-of-scope object. It's a way to : boost performance. Yes, you can always use a global scope variable to : maintain the reference, but if the object is pretty large and you are : not sure whether you will use it in the future, weakreference is very : useful. say, you have a big array of 1GB elements, you need to read : the content from disk I/O. If you need to do some operation on this : array and get some result several times but don't always need it : there, you can main
|
k*k 发帖数: 508 | 10 i just mention a situation the weak reference can be used, the
important part is not the size of the array, but the slow disc I/O
【在 p*u 的大作中提到】 : : ~~~~~~~~~~~~~~who will put it on stack???
|
|
|
k*k 发帖数: 508 | 11 even if the data is stored in the heap, there's the same problem
when you don't know when to release the resource, you need to maintain
the memory you allocated in the heap. Or you have to do the costly
initialization operation time and time again.
【在 k*k 的大作中提到】 : i just mention a situation the weak reference can be used, the : important part is not the size of the array, but the slow disc I/O
|
p*u 发帖数: 2454 | 12 in that case, keep it on heap until it's never needed. having it controled
by urself is always better than trying to beat GC.
【在 k*k 的大作中提到】 : even if the data is stored in the heap, there's the same problem : when you don't know when to release the resource, you need to maintain : the memory you allocated in the heap. Or you have to do the costly : initialization operation time and time again.
|
k*k 发帖数: 508 | 13 it's not beating GC, it's taking advantage of GC
You still have the control.
【在 p*u 的大作中提到】 : in that case, keep it on heap until it's never needed. having it controled : by urself is always better than trying to beat GC.
|
p*u 发帖数: 2454 | 14 u need to consider the opportunity cost. if GC has collected the object, u
need to load it again.
【在 k*k 的大作中提到】 : it's not beating GC, it's taking advantage of GC : You still have the control.
|
k*k 发帖数: 508 | 15 that's the key point. You can maintain a big array in heap, but there
is often the case that you need to release it to give memory to other
modules, and you don't know when to release it. A weakreference can be
maintained until the memory is really released. You don't need to care
about when exactly to delete the array. You just need to check before
if it's still there before using it.
in other words, weak reference can minimize the chance of doing the
costly initialization.
【在 p*u 的大作中提到】 : u need to consider the opportunity cost. if GC has collected the object, u : need to load it again.
|