C++ Function Pointers Simplified!

18 10 2009

Background information
Pointer is a variable which holds the address of another variable. Where, function pointer is again a variable which holds the address of a function.

If you think pointers are evil, then function pointers must be Satan for you. 🙂 Well, is there any easy way to create function pointers from function prototype? Indeed, there is. Its the “BAT” technique. Never heard about it before? No problem. Its invented by me just now. After watching BatMan series from cartoon network.

FunctionPointers


The “BAT” technique is this –

  1. Put Bracket or parenthesis around the function name.
  2. Add Asterisk or star in-front of function name.
  3. Now Typedef it to create a new datatype. Means change the function name to new datatype name and add typedef infront of it.
  4. Now you can use the new function pointer datatype like ordinary variables.

For instance, Assume we want to make a function pointer for function – DWORD MyFunction( int a, int b).

1) Bracket
DWORD (MyFunction)( int a, int b);

2) Asterisk
DWORD (*MyFunction)( int a, int b);

3) Typedef
typedef DWORD (*MyFunctionPtr)( int a, int b);

Ah! you have created a function pointer – MyFunctionPtr for function type – ‘DWORD MyFunction( int a, int b)’
Now you can use it like any other variable in your code. For instance, just see the following code snippet with real world usage of function pointers.

// Callback function for progress notification.
bool NotifyProgress( int Percentage )
{
 // Display progress and return true to continue.
 return true;
}

// typedef function pointer.
typedef bool (*NotifyProgressPtr)( int Percentage );

// DVD Burning function with pointer to NotifyProgress
// to update progress.
void BurnDVD( NotifyProgressPtr FnPtr )
{
 for( int Progress = 0; Progress <= 100; ++Progress )
 {
 // Call the function.
 (*FnPtr)(Progress);
 }
}

// Main function.
int _tmain(int argc, _TCHAR* argv[])
{
 // Ummm... Burn one DVD.
 BurnDVD( NotifyProgress );
 return 0;
}


Function pointers are not that much evil. Isn’t it? 😉


Targeted Audience – Beginners.

Advertisements




How to Set Dialog as TopMost Window?

11 10 2009


I always wondered about popularity of Winamp. It has rich custom drawn UI, which made it stand out of the crowd. Did you noticed its “Always on top” feature and wondered about how its implemented? Its time to reveal the secret – How winamp implemented that feature – Staying at the top?

SetTopMostWindow


You can use – SetWindowPos() with HWND_TOPMOST flag. Have a look at the code snippet.

void CRabbitDlg::OnSetTopmost()
{
    // Set window position to topmost window.
    ::SetWindowPos( GetSafeHwnd(),
                    HWND_TOPMOST,
                    0, 0, 0, 0,
                    SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSIZE );
}


Single line of code. But wowing feature. isn’t it?


Targeted Audience – Beginners.





Microsoft MVP for Visual C++ for 2009-2010

5 10 2009

Dear All,

With great pleasure, I would like to inform you that – with god’s grace, I’ve been awarded as Microsoft MVP (Most Valuable Professional) for Visual C++. At this moment I would like to thank all my readers for supporting me and making this happen! Please go through my MVP profile.

MicrosoftMvp

I really thank Microsoft for this wonderful MVP award and I really love their products. I really feel great about being a part of it. Well, I owe all this achievements to my fellow readers who motivated me to post more and more and atlast ended in being an MVP. So hearty thanks to you all, once again!

Best Regards,
Jijo. [Humble VC++ enthusiasit from Next Door]