Solved

Passing a member function for a function pointer argument

Posted on 2004-04-22
8
431 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

757 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

22 Experts available now in Live!

Get 1:1 Help Now