Solved

C++ object method called from C

Posted on 1998-12-10
3
251 Views
Last Modified: 2010-04-02
I have to mix up C library functions and C++ objects.
At some point I wish that a C function calls a C++ method.

The problem is that the C functions is called from a .c compiled file.

This is an example of the cf function called from a function defined in a .c file.
cf( void* p ){
   ((Class*)p)->method();
}

Apparently it is possible to call C functions from C++ but not the reverse.

Any help would be warmly welcomed.
0
Comment
Question by:meessen
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
stsanz earned 200 total points
ID: 1179818
Try defining an exported C function in your C++ files to wrap up the object method, so you can call C from C.

Declare this wrapper function as :
extern "C" MyWrapperFunction (...)
{ ... }


0
 
LVL 1

Author Comment

by:meessen
ID: 1179819
Ok. Just found out my self in the same time.
You gave the right answer.

I thought that using extern "C" would disallow to use method calls from within the encapsulated function. But apparently not. My test work.

Enjoy the points and the grade ;-)

0
 
LVL 6

Expert Comment

by:stsanz
ID: 1179820
extern "C" only defines how external parameters and return values are passed through, and how exported functions are named (the latter is different between C and C++)

Nevertheless, your extern "C" function is still C++ because it is defined in a CPP file.


 
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

  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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

803 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