Monday, June 11, 2007

Unique instance creator using templates C++

I am presenting an idea to create a unique instance of an object using templates.. This is very similar to Singleton design pattern..
The code may be look like this

class UniqueFactory

{

private :

   UniqueFactory()
   {
   }
public :

   static T * Instance()
   { 
        static T* ptr = new T();
        return ptr;
   }
};
So it will give the same instance of the object always.. For example
int *pInst = UniqueFactory<int>::Instance();
*pInst = 11;
pInst = UniqueFactory<int>::Instance();
*pInst , points to the same unique instance.
Another example is ( UserData can be any struct or class etc. )
UserData *pData = UniqueFactory<UserData>::Instance();
Hopes you may like it..!!

Thursday, June 7, 2007

Non-Polymorphic class And dynamic_cast

dynamic_cast, Non_polymorphic class , Some mfc thoughts
----------------------------------------------------------------------------------------

dynamic_cast couldn't be applied to "non-polymorphic" class . So what is this "non-polymorphic" class ? A class which has no virtual function is caled the non_polymorphic class. If you try to use dynamic_cast with non-polymorphic class. the result will be compile error. For example the code written below cause to an error.

class SuperBase
{
};
class Derived : public SuperBase
{
};


Derived* pDer = new Derived;
SuperBase* pBase = pDer;
Derived* pTest = dynamic_cast<Derived*>(pBase);

This code will try to cast pBase to Derived* , but it will not compile. To correct it use static_cast.

Derived* pTest = static_cast<Derived*>(pBase);

See the code below
CWnd *pWnd = GetDlgItem(IDC_BUTTON1); // IDC_BUTTON1 is a windows button control in a dialog.

Try to cast this pWnd to CButton* with dynamic_cast will give you a null pointer, like dynamic_cast<CButton*>(pWnd) will give you a null pointer. Means the cast is not working . Why ?????

Because mfc is creating some temporary objects inside , to reduce overehad. I will try to explain this later....


Sunday, June 3, 2007

Difference Between Parent And Owner windows

Parent , Owner Windows Vc++
--------------------------------------------

We all know the relation ship between parent and child windows.
So if i ask you this question "What is the difference between Owner window andv owned window' ?? If you know the answer leave now.. No need to read the rest of the page. Otherwise you may find it interesting to read.

I don't want to write more about this topic.. Since this is a simple question..
Child window means , its drawing is limited to the parent region , and also it z order is also changed according to parent.

But if you consider owner windows it can draw outside the parent window region ,
But if you close the main Top-level window , both of these windows will close..

So when creating a child window using CreateWindow function , there is an option for providing parent for the window.

If you are creating a child window with WS_POPUP style that window automatically becomes a owner window, and it can draw outside the region)(means (o,0) position indicates top Left corner of desktop). Otherwise it is a normal child window.
That's it...