• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

Passing a member function for a function pointer argument

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
jacobselvin
Asked:
jacobselvin
1 Solution
 
stefan73Commented:
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
 
agri_amitCommented:
Add a function pointer variable to your class and initialize it with the function, which u want to be called .

regards,
amit
0
 
rendaduiyanCommented:
use static member instead.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
itsmeandnobodyelseCommented:
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
 
stefan73Commented:
itsmeandnobodyelse,
> int MyClass::errFunc()
Should be static.

Stefan
0
 
itsmeandnobodyelseCommented:
>>     static int           errFunc();

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

Regards, Alex
0
 
guntherothkCommented:
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
 
itsmeandnobodyelseCommented:
Gunther,

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

Regards, Alex
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now