Click here to Skip to main content
12,511,920 members (51,819 online)
Click here to Skip to main content

Stats

29.5K views
2.8K downloads
53 bookmarked
Posted

Secure Function Pointer and Callbacks in Windows Programming

, 4 May 2011 CPOL
This article explains the usage of function pointer and callback in Windows application programming Interface (API).
// CPPFunctionPointer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
using namespace std;

class CFunctionPointer{
private:
   int y;
   static int x;

public:
   void SetData(int value) 
   {
	   y = value; 
	   return;
   }
   
   int GetData() 
   {
	   return y;
   }
   
   static void SSetData(int value) 
   {
	   x = value; 
	   return;
   }
   static int SGetData() 
   {
	   return x;
   }


   public:
    void foo()
	{
		cout<<"foo called"<<endl;
	}
    
	void foo1(int x)
	{
		cout<<"foo1 called with x "<<x<<endl;
	}
    
	void TestingMethod();
    
	void (CFunctionPointer::*Ptr2Func)(void);
};


void CFunctionPointer::TestingMethod()
{
    Ptr2Func = &CFunctionPointer::foo;
    (this->*Ptr2Func)();
}

int CFunctionPointer::x = 0;

int _tmain(int argc, _TCHAR* argv[])
{
	CFunctionPointer mydata, mydata2;

   // Initialize pointer.
   void (CFunctionPointer::*pmfnP)(int) = &CFunctionPointer::SetData; // mydata.SetData;

   // Initialize static pointer.
   void (*psfnP)(int) = &CFunctionPointer::SSetData;

   mydata.SetData(5); // Set initial value for private data.
   cout << "mydata.data = " << mydata.GetData() << endl;

   (mydata.*pmfnP)(20); // Call member function through pointer.
   cout << "mydata.data = " << mydata.GetData() << endl;

   (mydata2.*pmfnP)(10) ; // Call member function through pointer.
   cout << "mydata2.data = " << mydata2.GetData() << endl;

   (*psfnP)(30) ; // Call static member function through pointer.
   cout << "static data = " << CFunctionPointer::SGetData() << endl ;


   cout << endl << endl;

    CFunctionPointer A;
    A.TestingMethod();    


	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

R.selvam
Software Developer (Senior)
India India
Selvam has worked on several technologies like Core Java, VC++, MFC, Windows API and Weblogic server. He takes a lot of interest in reading technical articles and enjoy writing them too. He has been awarded as a Microsoft Community Star in 2004, MVP in 2005-06, SCJP 5.0 in 2009, Microsoft Community Contributor(MCC) 2011.

Github : https://github.com/selvamselvam
Web site: http://www.careerdrill.com

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160929.1 | Last Updated 4 May 2011
Article Copyright 2011 by R.selvam
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid