I'm trying to use c++ objects from my c code. I need to create the c++ object, and make multiple calls from the c code to the methods in the class.
Here's what I've implemented:
In the C file I created a structure to be the pointer to the c++ class:
In that same file, I made a call to a c function in the c++ file passing that pointer:Code:/* create a pointer that will actually be a pointer to the c++ object */ struct myClass* pMyStruct;
In the cpp file is the c++ wrapper function as well as the myClass class. Is this right? Is this how the object is created and the reference to it is set?:Code:call_class_init(pMyStruct);
And then in the init_class_obj() method I do a bunch of class implementation stuff of the c++ class.Code:extern "C" void call_class_init(myClass* p1) { myClass fred; p1= &fred; p1->init_class_obj(); }
So then in the C file I can make other calls to the c++ class by having more extern C methods to call using the same pointer:
and in the c++ file:Code:set_count(pMyStruct, 4);
Does this all make sense? Is there an easier way? With my implementation the 1st call works, and then when I make another call to the c++ object, it fails.Code:extern "C" void set_count(myClass* p1, int count) { p1->set_count(count); }
any help, sample code, or links would be great. I did a search for "mix c c++" and found a few results via google.
Thanks!



LinkBack URL
About LinkBacks


