Board index » cppbuilder » R: R: How to get access on TMemo Canvas ?

R: R: How to get access on TMemo Canvas ?


2003-06-27 08:46:07 PM
cppbuilder78
Hans Galema < XXXX@XXXXX.COM >wrote in
message 3efc3813$ XXXX@XXXXX.COM ...
Quote
Steve_Aletto wrote:

Do your choice.
Done. What's yours? :-)
Steve.
 
 

Re:R: R: How to get access on TMemo Canvas ?

Damon Chandler (TeamB) < XXXX@XXXXX.COM >wrote in message
XXXX@XXXXX.COM ...
Quote
Steve,
The auto_ptr class assures that the TControlCanvas
object is destroyed
when the variable (C, in this case) goes out of scope.
It's important to
All clear Damon, but I have another question: it seems
auto_ptr is more powerful than new/delete, so, why or when
to use new/delete?
As I asked to Hans in an earlier post, what do you use
normally? New/delete or auto_ptr? I'm used to use
new/delete, but if there's a better choice...
TIA,
Steve.
 

Re:R: R: How to get access on TMemo Canvas ?

Steve,
Quote
All clear Damon, but I have another question: it seems
auto_ptr is more powerful than new/delete, so, why or when
to use new/delete?
There are cases in which you shouldn't use the auto_ptr class. For example,
because auto_ptr deletes the memory using the 'delete' operator, you don't
want to use auto_ptr with pointers whose memory needs to be deleted with
'delete []' (e.g., when allocating an array with 'new []'; see
www.gotw.ca/gotw/042.htm) In addition, you have to be careful when
copying auto_ptr's because ownership is transferred from the copied object
to the copying object. For example...
class TFoo
{
public:
TFoo() { ShowMessage("Creating TFoo"); }
~TFoo() { ShowMessage("Destroying TFoo"); }
};
#include <memory>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TFoo* RawFoo = new TFoo();
std::auto_ptr<TFoo>AutoFoo(RawFoo);
{
std::auto_ptr<TFoo>AutoFoo2 = AutoFoo;
}
ShowMessage("End of inner block");
// At this point...
// (1) AutoFoo2 has called 'delete RawFoo'.
// (2) 'AutoFoo.get()' reports NULL because
// ownership has transferred to AutoFoo2.
// (3) Don't dereference RawFoo because
// AutoFoo2 has destroyed it!
}
Here, ownership of RawFoo is transferred first to AutoFoo, and then to
AutoFoo2. At the end of the inner block where AutoFoo2 goes out of scope,
you need to be careful not to access RawFoo because it has been destroyed by
AutoFoo2. The output of the above code should be "Creating TFoo",
"Destroying TFoo", and then "End of inner block". Ownership transfer is
also the reason you shouldn't use auto_ptrs with container classes that
might make internal copies (e.g., upon a sort of the items); see
www.gotw.ca/gotw/025.htm. The Boost library has some smart-pointer
classes that address this (and the array) issue(s):
www.boost.org/libs/smart_ptr/.
Quote
As I asked to Hans in an earlier post, what do you use
normally? New/delete or auto_ptr? I'm used to use
new/delete, but if there's a better choice...
I normally use auto_ptr when possible; for arrays and containers, I use new
and delete.
--
Damon (TeamB)
BCBCAQ - bcbcaq.bytamin-c.com
 

{smallsort}