> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
> Behalf Of Wilfried Mestdagh
> Sent: 13 January 2007 17:07
> To: twsocket@elists.org
> Subject: Re: [twsocket] WinSock error 10035 with TWSocketServer
> 
> Hello Bevan,
> 
> I'm not a CBuilder specialis but:
> 
> >   AnsiString msg = ((TWSocketClient*) Sender)->ReceiveStr();
> 
> This is a good typecast. It tells the compiler that Sender is of type
> TWSocketClient, so compiler can do the exact address offset
> calculations within the type. You still have to check if msg is > "".
> 

Its ok if you are sure that Sender is a TWSocketClient.  Otherwise its very
unsafe.

> >   TWSocketClient* Client = dynamic_cast<TWSocketClient*>(Sender);
> >   AnsiString msg = Client->ReceiveStr();
> 
> The first line will execute, I think the second line will generate an
> exception here. I'm not sure. Did you have it into an exception block ?
> 
> As far as I know the dynamic_cast is a kind of equivalent for 'is' or
> 'as' operators in Delphi and some other languages. It is not the same as
> a type cast. I even think it will evaluate to false (null) because
> Sender is of type TObject (yeah it "is" TWSocketClient, but compiler
> dont know that).

A dynamic cast is safer.  Yes its similar to Delphi's as/is operators.  In
this case, this a pointer dynamic cast (rather than a reference), it will
return null/nil if Sender isn't really a TWSocketClient, and the code should
then check for this.  An alternative is to use a reference:

 TWSocketClient& Client = dynamic_cast<TWSocketClient&>(Sender);

which will throw an exception if Sender isn't a TWSocketClient at runtime.
This is the safest way, it's better to have an exception than to treat an
object as a different class (which could result in memory corruption).

Dan


-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://www.elists.org/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to