Solved

Passing a member function for a function pointer argument

Posted on 2004-04-22
8
433 Views
Last Modified: 2008-03-03
I'm writing a C++ application using Sybase Client library. Sybase client library has a method called "dberrhandle(FARPROC)" which takes a function name as argument. This function is called when there is some error. This function will ideally have the error handing routine. When I have this function as a normal function ( not a member function ) it works fine. But it spoils my design as I have everytinhg else in a class. So I have to declare some global variable to know that something was wrong. Is there any way I can have this function as a member function inside the same class and still register this function as a error handling routine.
0
Comment
Question by:jacobselvin
8 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10897187
Hi jacobselvin,
> Is there any way I can have this function as a member function inside
> the same class and still register this function as a error handling routine.

No, you can't. Member functions always have "this" as an implicit first argument. You could check, though, if you can pass a static member function.

BTW: What you're trying to pass would be a delegate, a combination of "this" and a function pointer. This is not explicitly supported by C++.

Cheers,
Stefan
0
 

Expert Comment

by:agri_amit
ID: 10897568
Add a function pointer variable to your class and initialize it with the function, which u want to be called .

regards,
amit
0
 
LVL 5

Expert Comment

by:rendaduiyan
ID: 10897757
use static member instead.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 125 total points
ID: 10897941
If you have only one Sybase connection running at a time you mey help you using that construct:

class MyClass
{

public:
     MyClass()   { m_pCurrent = this; }   // do that for all constructors
     ....
public:
     static MyClass* m_pCurrent;
     static int           errFunc();

};


// myclass.cpp

MyClass* MyClass::m_pCurrent = Null;

int MyClass::errFunc()
{
      if (m_pCurrent != NULL)
      {
            ....
      }
}

There is no better solution as the error func takes no argument.

If there are more than one connections running in different threads and you have a MyClass instance for any thread, this won't work. You may hold a static list of all instances of MyClass instead a single pointer but i don't know how to find out which might be the right one.

Regards, Alex
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Expert Comment

by:stefan73
ID: 10898092
itsmeandnobodyelse,
> int MyClass::errFunc()
Should be static.

Stefan
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10898152
>>     static int           errFunc();

It is static!! I don't need static keyword in cpp.

Regards, Alex
0
 
LVL 2

Expert Comment

by:guntherothk
ID: 10900879
You can use a static member function. This will keep your code encapsulated in the class.

You may want access to class instance members, which you can't get from a static function. I don't know what the signature of a FARPROC is in your library, but it's typically something like int (*f)(void*). If so, you can pass a pointer to the invocation of the handler function. If you pass a pointer to your class instance of choice, you can cast the void* to a pointer-to-class. Then your static member function can call any member function (including private methods) of the instance.

If FARPROC doesn't have an argument, you could stash a pointer to the class in a static member variable, then your static FARPROC could get to the class instance in that way. If your code is multithreaded, you'd have to protect this resource with a mutex. If your class instance is a singleton, you may already have this pointer lying around.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10900963
Gunther,

FARPROC function has no argument. I checked it with Google.

Regards, Alex
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now