Solved

C++ object method called from C

Posted on 1998-12-10
3
254 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
[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
  • 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

Industry Leaders: 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!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 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