Article View: borland.public.cppbuilder.nativeapi
Article #11Re: Assigning TGraphic to descendent class
From: Thomas Maeder [TeamB]
Date: Fri, 23 Jul 2004 17:37
Date: Fri, 23 Jul 2004 17:37
91 lines
4249 bytes
4249 bytes
"ScOe" <cbinders@hotmail.com> writes:
"Andrue Cope [TeamB]" <no.spam@spammy.com> writes:
> Questions on using VCL components should be asked in
> .VCL.COMPONENTS.USING. Follow-up set.
Agreed.
But there is a big C++ problem with the posted code, which is best dealt with
in .language.cpp.
> void __fastcall TCBZIconObject::SetImage (TGraphic *Value)
> {
> try {
> FImage = new Graphics::TBitmap ();
> FImage->Assign (Value);
> }
> catch (...) {
> try {
> delete FImage;
> FImage = new Graphics::TIcon ();
> FImage->Assign (Value);
> }
> catch (...) {
> }
> }
> }
>
> is it ok to do it like this ?
No.
What you seem to want to do are the following operations:
1) allocate dynamic memory for a Graphics::TBitmap object
2) construct a Graphics::TBitmap object into that memory using
Graphics::TBitmap's default constructor
3) assign the address of a TGraphic object to that Graphics::TBitmap object
using the Assign() member function
Some of these operations may fail (I know for sure that the first one can, and
that's sufficient for my point); failure is signaled by an exception.
The first problem I see is that the exception handler part of SetImage()
attempts to redo the things that just failed. You'll have to work very hard
to convince me that this is the right thing to do. Why on earth should these
operations succeed after they they have just failed?
Then to the FImage data member. It seems to be used to sometimes own a
dynamically created Graphics::TBitmap object and sometimes not. From the
code you post, it further seems that FImage is of type Graphics::TBitmap *.
However it is very rarely a good idea to express the ownership of a
dynamically allocated resource through a "raw" pointer. The simplest and
therefore usually best thing is to not use a pointer at all, but a "real"
object instead. If that is not possible (because of VCL or other
restrictions), use a smart pointer object (such as a std::auto_ptr).
One of the benefits of not using a raw pointer is that you don't need to
write exception handling code except when you are handling exceptions. Making
FImage a std::auto_ptr<Graphics::TBitmap> would simplify the SetImage()
member to something like
void __fastcall TCBZIconObject::SetImage(TGraphic *Value)
{
FImage.reset(new Graphics::TBitmap);
FImage->Assign(Value);
}
This would also let a possible exception escape to the caller, which is a
Good Thing (tm).
Finally, I can't imagine that SetImage() should behave the same way when FImage
already owns an object and when it doesn't. My version of SetImage already
deals correctly with this issue; reset() will delete the currently owned
object before "adopting" the new one. But still, I think you should either
initialize FImage to own an object in the constructor(s) or add a check at the
beginning of SetImage(), as in
void __fastcall TCBZIconObject::SetImage(TGraphic *Value)
{
if (FImage.get()==0)
FImage.reset(new Graphics::TBitmap);
FImage->Assign(Value);
}
Technical Details
Message-ID:
<m27jsumxpp.fsf@madbox2.local>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news.zanker.org!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<410111e9@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,d2b0b035e2ea3f51 X-Google-Thread: fde74,c4710d1a60ad35ee,start X-Google-Attributes: gid10cc67,gidfde74,public X-Google-Language: ENGLISH,ASCII-7-bit Newsgroups: borland.public.cppbuilder.nativeapi,borland.public.cppbuilder.language.cpp Followup-To: borland.public.cppbuilder.language.cpp Subject: Re: Assigning TGraphic to descendent class References: <410111e9@newsgroups.borland.com> From: maeder@glue.ch (Thomas Maeder [TeamB]) Organization: TeamB Date: Fri, 23 Jul 2004 19:37:22 +0200 Message-ID: <m27jsumxpp.fsf@madbox2.local> User-Agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Security Through Obscurity, linux) Cancel-Lock: sha1:WW0EjFwIxN0zCczl99uXdndNfjo= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii NNTP-Posting-Host: 217.162.3.148 X-Trace: newsgroups.borland.com 1090604242 217.162.3.148 (23 Jul 2004 10:37:22 -0700) Lines: 91 X-Authenticated-User: tmaeder Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news.zanker.org!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail