Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This technique will work in some cases where standard data types are incompatible, but in others it will fare no better: name mangling differences and class memory layout differences (vptrs and tags) come to mind.</p> <p>This is why C APIs are preferred.</p> <p>But you <em>can</em> improve usability by burying the C API where the library caller never needs to see it. Then add a thin, idiomatic C++ overlay that provides the visible library interface. In some cases the thin overlay code can be used on both caller and library side, each compiled in its own environment: flags, link conventions, etc. Only C data types are exchanged. The simpler these data types are, the more compatibility you'll obtain.</p> <p>The layer also takes care that memory allocation and deallocation occur on the same side of the API on a per object basis, as your code does. But it can be more flexible. For example, it's possible to arrange for an object allocated in the caller to be deallocated in the library and vice versa.</p> <pre><code>// library.h for both caller and library // "shadow" C API should not be used by caller extern "C" { void *make_message(char *text); char *get_text_of_message(void* msg); void send_message(void *msg); // destroys after send. } // Thin C++ overlay class Message { void *msg; public: Message(const std::string &amp;text) { msg = make_message(text.c_str()); } void send() { if (msg) send_message(msg); else error("already sent"); msg = 0; } std:string getTextString() { return std:string(get_text_of_message(void* msg)); } } </code></pre>
    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