Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ faq 32.8 "pass an object of a C++ class to/from a C function" questions
    primarykey
    data
    text
    <p><a href="http://www.parashift.com/c%2B%2B-faq/cpp-objs-passed-to-c.html" rel="nofollow">http://www.parashift.com/c%2B%2B-faq/cpp-objs-passed-to-c.html</a></p> <p>The answer is in two parts. First part i guess is till the codes are over.</p> <p>For the first part, what i see is that the author is using 'C' linkage directive(extern "C") for either functions written in C or C++ functions to be called by C functions (see below), so link will be ok.</p> <pre><code>extern void c_function(Fred*); // c function extern Fred* cplusplus_callback_function(Fred*); // c++ function </code></pre> <p>This make sense. </p> <p>Then Fred* is passed all the way from main() to c_function() [i guess this is what author/faq says about passing C++ object to C function], and then to cplusplus_callback_function() [object passed back to C++ domain from C function]. </p> <p>So first question, am i understanding the above correctly? So the answer to the faq is just "pass a pointer" of the object?</p> <p>Second question, what the author explained after the code lost me. He suddenly talk about comparing if pointers are equal, pointer conversion between base and derived etc. What is he trying to say? That pointer after being passed to c_function, lost all its magic? Were it a base class pointer pointing to a derived class, we can do a lot more if it's passed to C++ functions than C function?</p> <p>Last question, i guess i do not know if C++ code is wrapped in extern "C" directive, the only thing changed is linkage but nothing else? C++ code is still C++, right? if i write all codes in C++, but deliberately wrap some functions in extern "C", what do i lose?</p> <p>Any help?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload