<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Mixing C and C++ APIs</title> <link rel="stylesheet" type="text/css" href="style.css"> <meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> <link rel="home" href="index.html" title="Programming with gtkmm 3"> <link rel="up" href="chapter-basics.html" title="Chapter 3. Basics"> <link rel="prev" href="sec-intermediate-types.html" title="Intermediate types"> <link rel="next" href="sec-helloworld.html" title="Hello World in gtkmm"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr><th colspan="3" align="center">Mixing C and C++ APIs</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="sec-intermediate-types.html"><img src="icons/prev.png" alt="Prev"></a> </td> <th width="60%" align="center">Chapter 3. Basics</th> <td width="20%" align="right"> <a accesskey="n" href="sec-helloworld.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> </table> <hr> </div> <div class="sect1"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="sec-basics-gobj-and-wrap"></a>Mixing C and C++ APIs</h2></div></div></div> <p>You can use C APIs which do not yet have convenient C++ interfaces. It is generally not a problem to use C APIs from C++, and <span class="application">gtkmm</span> helps by providing access to the underlying C object, and providing an easy way to create a C++ wrapper object from a C object, provided that the C API is also based on the <code class="classname">GObject</code> system.</p> <p> To use a <span class="application">gtkmm</span> instance with a C function that requires a C <code class="classname">GObject</code> instance, use the C++ instance’s <code class="function">gobj()</code> function to obtain a pointer to the underlying C instance. For example: </p> <pre class="programlisting"> Gtk::Button* button = new Gtk::Button("example"); gtk_button_do_something_that_gtkmm_cannot(button->gobj()); </pre> <p> </p> <p> To obtain a <span class="application">gtkmm</span> instance from a C <code class="classname">GObject</code> instance, use one of the many overloaded <code class="function">Glib::wrap()</code> functions. The C instance’s reference count is not incremented, unless you set the optional <em class="parameter"><code>take_copy</code></em> argument to <code class="literal">true</code>. For example: </p> <pre class="programlisting"> GtkButton* cbutton = get_a_button(); Gtk::Button* button = Glib::wrap(cbutton); button->set_label("Now I speak C++ too!"); </pre> <p> The C++ wrapper shall be explicitly deleted if </p> <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> <li class="listitem"><p>it's a widget or other class that inherits from <code class="classname">Gtk::Object</code>, and</p></li> <li class="listitem"><p>the C instance has a floating reference when the wrapper is created, and</p></li> <li class="listitem"><p><code class="function">Gtk::manage()</code> has not been called on it (which includes if it was created with <code class="function">Gtk::make_managed()</code>), or</p></li> <li class="listitem"><p><code class="function">Gtk::manage()</code> was called on it, but it was never added to, or was later removed from, its parent.</p></li> </ul></div> <p> <code class="function">Glib::wrap()</code> binds the C and C++ instances to each other. Don't delete the C++ instance before you want the C instance to die. </p> <p>In all other cases the C++ instance is automatically deleted when the last reference to the C instance is dropped. This includes all <code class="function">Glib::wrap()</code> overloads that return a <code class="classname">Glib::RefPtr</code>.</p> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="sec-intermediate-types.html"><img src="icons/prev.png" alt="Prev"></a> </td> <td width="20%" align="center"><a accesskey="u" href="chapter-basics.html"><img src="icons/up.png" alt="Up"></a></td> <td width="40%" align="right"> <a accesskey="n" href="sec-helloworld.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> <tr> <td width="40%" align="left" valign="top">Intermediate types </td> <td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td> <td width="40%" align="right" valign="top"> Hello World in <span class="application">gtkmm</span> </td> </tr> </table> </div> </body> </html>