473,915 Members | 4,409 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 4027
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
9389
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
2555
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
7334
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
8680
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
8140
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
4978
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
3365
by: nsharma78 | last post by:
Hi, I have a code as follows: class A { public: void print(){cout << "Magic" << endl;} };
43
2120
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
2426
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
10039
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
11354
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10923
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
11066
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,...
1
8100
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
7256
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4778
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3368
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.