Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ object method called from C

Posted on 1998-12-10
3
Medium Priority
?
256 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 800 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

618 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