Solved

C++ object method called from C

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 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

17 Experts available now in Live!

Get 1:1 Help Now