Solved

C++ object method called from C

Posted on 1998-12-10
3
250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

912 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now