calling C++ method from C code

Zohar81

I've read the following article here that explain how to call c++ based functions from pure C code.

The method is best explained with the following example :

// C++ code:
class C {
    // ...
    virtual double f(int);
};
extern "C" double call_C_f(C* p, int i) // wrapper function
{
    return p->f(i);
}

/* C code: */
double call_C_f(struct C* p, int i);
void ccc(struct C* p, int i)
{
    double d = call_C_f(p,i);
    /* ... */
}

However, I fail to understand if where do i define an instance that is pointed by 'p'. notice that in the C code, it is delivered as pointer to opaque struct, and in the C++ code it's assumed to be pointer to valid instance of class C, but i didn't see where this pointer is being initialized.

p.s. Of course that each code block that relate to different language is compiled with an appropriate compiler, and different object file is created.

Bryan Chen

You need similar wrapper methods to create and destroy the C++ object.

C++ code:

extern "C" C * create_c()
{
    return new C;
}

extern "C" void destroy_c(C *p)
{
    delete p;
}

C code:

// forward declarations (better to put those in a header file)
struct C;
struct C * create_c();
double call_C_f(struct C* p, int i);
void destroy_c(struct C *p);

struct C * p = create_c();
double result = call_C_f(p, i);
destroy_c(p);
p = NULL;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling C# method in C code

From Java

Calling a java method from c++ in Android

From Dev

Calling a C# method from JavaScript

From Dev

calling objective c method from callback

From Dev

Calling Rust method from C with array parameters

From Dev

Calling C++ method with callback from ObjectiveC

From Dev

calling method from a new thread C

From Dev

c++ calling method from subclass in superclass

From Dev

C# MVVM Calling ViewModel method from Code Behind using NotifyIcon

From Java

Calling C# code from Java?

From

Calling Go library from C Code

From Dev

Calling C code from C++ with using extern "C"

From Dev

Calling a template method from another template method in C++?

From Dev

C# Calling base method from interface method

From Dev

Calling method 2 classes down from current method C#

From Dev

Calling non-generic method from generic method in C#

From Dev

Calling c++ generic method from C#

From Dev

Calling C++ dll Method from C#

From Dev

Trouble with calling C++ functions from C code

From Dev

Calling C code from an R package, within C

From Dev

Calling C# code from visual C++

From Dev

What's the protocol for calling Raku code from C code?

From Dev

Calling, and awaiting, an F# async method, from C#

From Dev

Calling a static Swift Method from within Obj-C

From Dev

Calling a C library method from swift using pointers

From Dev

Calling a method from another class - SpriteKit/Objective C

From Dev

Calling C# interface default method from implementing class

From Dev

Calling a C# overloaded method with out parameters from F#

From Java

Android: Calling java method with byte[] parameter from c++

Related Related

HotTag

Archive