?
Solved

Passing a member function for a function pointer argument

Posted on 2004-04-22
8
Medium Priority
?
438 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 375 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
 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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…
  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 …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

765 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