Solved

Passing a member function for a function pointer argument

Posted on 2004-04-22
8
435 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 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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simple way to manipulate chinese characters in non unicode mfc c++ application. 9 226
C++ get user from AD  (VS6) 7 95
C++ mouse_event mouse look 7 112
C++ Code Issue 4 38
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

733 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