Wednesday, January 23, 2008

Converting int to string(CString) in MFC

int nVAl = 10;
CString strConverter;
strConverter.Format("%d",nVal);
::AfxMessageBox(strConverter);

another method is native itoa

int nVAl = 10;
char strBuf[3];
_itoa( nVal,strBuf,3,10); // here 10 is radix
CString strForCnvrsn(strBuf);
::AfxMessageBox(strForCnvrsn);

Monday, January 21, 2008

Can we use dynamic cast instead of QueryInterface?

The dynamic_cast operator

It combines the type check with the cast, and uses the syntax of the new keyword casts It is only legal for pointers and references to polymorphic types; it will fail to compile for non-polymorphic and incomplete types. For a pointer, a successful cast will result in a pointer correctly cast and adjusted to the applicable part of the object – including multiple inheritance and virtual base class cases – whereas an unsuccessful cast will result in a testable null pointer:

base *b = ...;
derived *d = dynamic_cast(b);
if(d)
... // successful cast
else
... // unsuccessful cast


As dynamic_cast checks that the cast is possible, rather than checking for an exact type match, it is our substitute for an is-kind-of operation.


A dynamic_cast is normally presented as a safe downcast mechanism, but this is a very narrow view of its capabilities: it may also be used for safe crosscasting to sibling classes. In this context the role of dynamic_cast is to query interfaces


Using dynamic_cast to query interface support.
document *d = new wp_document;
...
if(storable *s = dynamic_cast(d))
... // use storable operations for d
if(notifiable *n = dynamic_cast(d))
... // will not be executed for d