473,721 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C function pointer within a class method


Hello,

I am trying to use a library which has a function, lets call it an
external function, which wants a function pointer as one of its
arguments.

If I use that library in a C program, all works well and I have no
problems. But I used the C program only to get started with the
library. The actual program I want to use that library with is in C++.

In my C++ program, I have a method function whose pointer I want to
use as the function pointer in the external function. However, I am
running in to problems. g++ gives this error (source code is given
below):
$g++ -ansi -pedantic -Wall -o classfoo classfoo.cc
classfoo.cc: In member function 'void ClassFoo::Metho d()':
classfoo.cc:30: error: cannot convert 'void (ClassFoo::*)(i nt*)' to
'void (*)(int*)' in assignment
Below is an example program which demonstrates the above problem. In
this, vFunc1 is the external function which I cannot change (for the
curious, I am trying to use this library: http://www.ics.forth.gr/~lourakis/levmar/,
and the function I am trying to use is dlevmar_dif on that web page).
//------------------------ classfoo.cc
---------------------------------------
#include <iostream>

//a pre-defined function; not to be changed
void vFunc1(void (*func)(int *j), int i){
int m=0;//initialized to certain value
func(&m);
std::cout << m + i << std::endl;
}

//our example class
class ClassFoo{
public:
int q;
ClassFoo();
void Method();
void ModMethod(int *j);
};

ClassFoo::Class Foo(){
q = 2;
}
void ClassFoo::Metho d(){
void (*FuncPtr)(int *) = NULL;
FuncPtr = &ClassFoo::ModM ethod;
//vFunc1(FuncPtr, q);
}

void ClassFoo::ModMe thod(int *j){
(*j) = (*j) + 2;//modify value
return;
}

int main(){

ClassFoo EgClass;
EgClass.Method( );
return 0;
}
//---------------------------------------------------------------------------------
So, how do I get around this problem?

->HS

Jul 18 '07 #1
2 4017
hs******@gmail. com wrote:
Hello,

I am trying to use a library which has a function, lets call it an
external function, which wants a function pointer as one of its
arguments.

If I use that library in a C program, all works well and I have no
problems. But I used the C program only to get started with the
library. The actual program I want to use that library with is in C++.

In my C++ program, I have a method function whose pointer I want to
use as the function pointer in the external function. However, I am
running in to problems. g++ gives this error (source code is given
below):
$g++ -ansi -pedantic -Wall -o classfoo classfoo.cc
classfoo.cc: In member function 'void ClassFoo::Metho d()':
classfoo.cc:30: error: cannot convert 'void (ClassFoo::*)(i nt*)' to
'void (*)(int*)' in assignment
So it should, a class member isn't a C linkage function, it has a hidden
this parameter and the wrong linkage type.

To pass a pointer to a function to a C library, you have to use a
vanilla function with extern "C" linkage

--
Ian Collins.
Jul 18 '07 #2
Ian Collins wrote:
hs******@gmail. com wrote:
>Hello,

I am trying to use a library which has a function, lets call it an
external function, which wants a function pointer as one of its
arguments.

If I use that library in a C program, all works well and I have no
problems. But I used the C program only to get started with the
library. The actual program I want to use that library with is in C++.

In my C++ program, I have a method function whose pointer I want to
use as the function pointer in the external function. However, I am
running in to problems. g++ gives this error (source code is given
below):
$g++ -ansi -pedantic -Wall -o classfoo classfoo.cc
classfoo.cc: In member function 'void ClassFoo::Metho d()':
classfoo.cc:30 : error: cannot convert 'void (ClassFoo::*)(i nt*)' to
'void (*)(int*)' in assignment
So it should, a class member isn't a C linkage function, it has a hidden
this parameter and the wrong linkage type.

To pass a pointer to a function to a C library, you have to use a
vanilla function with extern "C" linkage
Okay. I just did that. The only problem was that the function whose
pointer I am passing needed to use some member variables of the class.
That is why I had made that function as a member function as well.

Making that function (whose pointer I need) as vanilla function in C, I
got around the problem of using some member variables by saving them in
a structure and passing its pointer to the library function I am using.

It compiled ... the next stage is debugging.

Thanks for the help and explanation.
regards,
->HS

Jul 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
9370
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1, 2, func);
6
2540
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class called CUtils with a static method called print2std()
10
7307
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
10
8661
by: gmtonyhoyt | last post by:
It's been mentioned to me that, in standard c, that you can have structures behave very much like classes in relation to something to do with function calls within a structure. Now, I didn't get the details, but does someone have a clue of what this person perhaps was talking about? Do you think he just ment something such as function pointers? Or could I, much like a C++ Class, call a class function that has access to the structure...
26
8110
by: johkar | last post by:
I need to cancel the link and execute a function onclick of all the links within the span tag which has a class of "container" assigned. There will be only one span tag with this class applied. I know you can get a specific tag using document.getElementsByTagName('span'), but I am unsure how to get the one with the class="container". I know there is a getAttribute method, just need a pointer or two to put it all together. Once I know...
20
4951
by: alexandre.braganti | last post by:
Hello, First sorry for my poor English, I am French ;-) I've got a comprehension problem of what happend in one of the project i'm working on. Basically I've got a class gs_object than has got a VIRTUAL function createList(). This createList() function is overloaded in another class named ct_server that inherits gs_object. in my code, it looks something like that :
8
3348
by: nsharma78 | last post by:
Hi, I have a code as follows: class A { public: void print(){cout << "Magic" << endl;} };
43
2080
by: Tony | last post by:
I'm working with GUI messaging and note that MFC encapsulates the message loop inside of a C++ class member function. Is this somehow inherently less robust than calling the message loop functions within main? (It just doesn't "feel" right to me). Example 1: class MyProg { public:
5
2414
by: pauldepstein | last post by:
Hi all, I saw some code like this: unsigned short SomeFunc(unsigned short SomeNum, bool SomeBool, const SomeClass& SomeMem, bool(SomeClass::*AmemberFunctionOfSomeClass)(const unsigned short) const)
0
8860
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8738
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9234
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9150
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8028
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6678
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2599
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.