Jon Skeet said:
And what use are they? If you could say what you normally try to
achieve with them, we can explain what you'd do in the same situation
in C#.
Sure. If you will please take a look at the following example which
calculates rect areas
inside a control. If we could discuss this based on this example, it would
be helpful
for me to understand the scenario and how exactly C# approaches this.
//IPaintableRectSubscriber is an interface with one method - void
OnPaintableRect(int nIndex, const CRect& rcItem);
void CThumbNailCtrl::IteratePaintableRects(IPaintableRectsSubscriber*
pSubscriber)
{
//Calculate rect areas that need painted, call back to subscriber
pSubscriber->OnPaintableRect(nIndex, rcItem) ;
}
//
BOOL CThumbNailCtrl::OnEraseBkgnd(CDC* pDC)
{
CRect rcClient ;
GetClientRect(rcClient);
//Local class here - note that the functionality
//implemented is not of interest to any other part of my control
class CEraseBkHelper : public IPaintableRectSubscriber
{
CThumbNailCtrl& m_outer; CDC* m_pDC; CRgn m_clipRgn; CRect
m_rcClient;
public:
void OnPaintableRect(const int i, const CRect& rc)
{
m_pDC->ExcludeClipRect(&rc);//exclude painted areas from
erase.
}
}erasebkhelper(*this, pDC, rcClient);
}
//
void CThumbNailCtrl::OnPaint()
{
CPaintDC dc(this);
//Local class here - Note the different params passed in constructor
//so i can have isolated logic and again, none of this
//makes sense to anything outside this function.
class CPaintHelper : public IPaintableRectsSubscriber
{
CThumbNailCtrl& m_outer; CPaintDC& m_dc;
public:
void OnPaintableRect(const int i, const CRect& rc)
{
//do the real painting here.
}
}painthelper(*this, dc);
}
As you see from the comments, the local classes are giving me a
nice way to encapsulate my logic within the respective functions,
yet allows for subscribing to a callback from another function
or class, hence maximizing the reuse.