Hi everyone, Have a nice day, please help me following concern I know that, Function pointer in C++ can point to an address of global function, or static public function of a class For example: typedef float (MyFunction)(float a, float b); //Global function float add(float a, float b) { return a+b; } Class Math { public static float add(float a, float b) { return a+b; } } void main(char*args[]) { MyFunction*fp; //Point to global function fp = add; printf("%f",fp(1,2)) //Point to public static function fp = Math::add; printf("%f",fp(5,10)); } But it can not point to address of a function of a specified object. For example, function add in calss Math is not static Class Math { public float add(float a, float b) { return a+b; } } The following is impossible (compile error) void main(char*args[]) { MyFunction*fp; //Point to public function of specified object instance Math*m = new Math(); fp = m->add; printf("%f",fp(5,10)); } But it is the thing i really want. Because i want to know function add is executed in context of what object (in this case we can know it is object m). Above example is something like the concept Delegate in .NET. Is there any design pattern to make such a delegate for C++? Thanks, Tin,
lvantin
Posts
-
Function pointer point to a object member function in C++? -
Database is locked when executing insert multi rowHi all, I made a very simple program to manipulate with DB as below 1. I have a SQL Server DB. The database contains just only 2 tables 1.1 TEST1(KEYCODE, SEIHIND, TEXT1, TEXT2, TEXT3), KEYCODE is primary key, auto increase 1.2 TEST2(TEXT1, TEXT2, TEXT3) no key 2. I make a new MDB by MS Access 2007 and created link table to these above SQL DB via ODBC(SQL Server driver) 3. My program uses DAO 3.6 to insert data to SQL via MS Access 3.1 The bussiness is to select data from TEST2 and insert to TEST1 3.2 SQL is like INSERT INTO TEST1(SEIHIND, TEXT1,TEXT2,TEXT3) SELECT '1', TEXT1, TEXT2, TEXT3 FROM TEST2 4. To obtain above, i made a simple program like below: Private Sub Command1_Click() Dim defWsk As DAO.Workspace Dim dbs As DAO.Database Dim sqlStr As String Set defWsk = DBEngine.Workspaces(0) Set dbs = defWsk.OpenDatabase("F:\Tin\PQMC.MDB") defWsk.BeginTrans sqlStr = "INSERT INTO TEST1(SEIHIND, TEXT1,TEXT2,TEXT3) " & _ "SELECT '1', TEXT1, TEXT2, TEXT3 " & _ "FROM TEST2 " Debug.Print sqlStr dbs.Execute (sqlStr) defWsk.CommitTrans End Sub 5. Issue: The problem when execting above code is "data is not inserted to TEST1, TEST1 is blocked(I could not execute SELECT * FROM TEST1 in SQL Analyser)" When i close program, table is not locked(I could execute SELECT * FROM TEST1 in SQL Analyser) Notes: I take above SQL and execute in SQL Analyser. it's OK Anyone know how to solve this problem, please help me. Thank you very much for your support. Tin Le,
-
Get CPU Usage of each CPU for a process running on multi CPUs systemHi all, I have a problem about getting CPU Usage of each CPU for a process running on multi CPUs system. Currently, I only can get total CPU usage. My application is required to calculate CPU Usage of each CPU. Anyone know about this topic, Could you please help? Thanks very much Tin Le
-
How to indicate the buffer size before calling function recv, WSARecv in socket programming???Hi all, I wrote client-server applicaion communicating via socket. I met a big problem that I did not know which function can be use to indicate the buffer size will be received before calling function recv() or WSARecv(). I want to know this information to allocate the buffer enough to receive the message coming. For example: int bufSize; /*Do which thing here to indicate buffer size bufSize = ???*/ char*buffer = new char[bufSize]; recv(hSOCKET,buffer,bufSize,0); If we dont know this information before calling receiving function, the problem is we won't know how big the buffer size should is. Although, we can do: char buffer[100000]; but the size of coming message can be bigger or the coming message is too smaller than that. Any boddy know this problem, Can you help me? Thanks very much & best regards, Tin Le
-
How to restrict client application to create COM objectHi all, I wrote a server(VC++) having 2 COM object(A,B) & I want client application only can create COM object A and can not create COM object B. COM object B will be return by calling a function exported in an interface of COM object A. For example, client application written by VB Dim a = new A Dim b as B call a.getInstanceB(b) call b.doSomething Any body know what to do to make COM object B can not be created by client application? Thanks a lot, Tin Le,
-
SOAP initializeHi all, I do a job relating to SOAP initialize. Sometime, SOAP initialize fail because, SOAP uses Wininet library as default and uses the proxy setting in IE, and this setting was not correct . Some one recommended me to use Winhttp instead of Wininet. Dim Client Set Client = Server.CreateObject ("MSSoap.SoapClient30") Client.ClientProperty("ServerHTTPRequest") = True Call Client.mssoapinit("http://..../SampleWSDL.wsdl", "http://...../SampleWSML.wsml") If Client.ClientProperty("ServerHTTPRequest") = True, SOAP will use Winhttp instead of Wininet. But I don't know what differences between Winhttp and Wininet are? Any of u have experiences about this? Can u help me to answer following questions: 1. What are differences between WinInet and WinHTTP? (using in SOAP) 2. problems if we use WinHTTP. Client.ClientProperty("ServerHTTPRequest") = True Thanks very much for any your ideas, Tin Le
-
Memory leakI used Performance monitor, a tool is supported by Windows, to investigate this. Private Bytes of the application always increases when i used that code to allocate memory and never descreases when release it.
-
Memory leakI met a problem relating to use CArray in MFC. CArray m_pArr; CMyClass is very simple Class CMyClass { CString m_szName; CString m_szId; long m_i4Age; } allocate memory: for (int i=0;i<1000;i++) { m_Arr.Add(new CMyClass); } destroy memory for (int i = 0;i