Note that there are some explanatory texts on larger screens.

plurals
  1. POAvoid leaking out external types in a C++ class
    primarykey
    data
    text
    <p>I have a class defined in a header like so (abbreviated):</p> <pre><code>class CairoRenderer { public: CairoRenderer(); ~CairoRenderer(); ... protected: cairo_t* m_context; cairo_surface_t* m_surface; }; </code></pre> <p>cairo_t and cairo_surface_t are types defined by the <a href="http://cairographics.org/" rel="nofollow">Cairo</a> graphics library.</p> <p>The problem I have is that if I want to use this class or its derived classes from another library or application, I need to include the cairo headers as well because I am "leaking" the cairo types out through the CairoRenderer header. I want this class (or any subclass of it) in the same library to be usable externally without needing to include the cairo header or link against the cairo libraries.</p> <p>So the next thing I tried was to use the pimpl technique as per the <a href="http://en.wikipedia.org/wiki/Opaque_pointer" rel="nofollow">Wikipedia</a> example because it looked like it could do what I wanted to achieve:</p> <p>CairoRenderer.h (abbreviated)</p> <pre><code>class CairoRenderer { ... protected: struct CairoRendererImpl; CairoRendererImpl* m_pimpl; }; </code></pre> <p>CairoRenderer.cpp (abbreviated)</p> <pre><code>#include "CairoRenderer.h" #include "cairo.h" .... struct CairoRenderer::CairoRendererImpl { public: CairoRendererImpl() : m_surface(NULL), m_context(NULL) { } ~CairoRendererImpl() { cairo_surface_destroy(m_surface); cairo_destroy(m_context); } void Initialize(cairo_t* context, cairo_surface_t* surface) { m_context = context; m_surface = surface; } cairo_surface_t* surface() { return m_surface; } cairo_t* context() { return m_context; } private: cairo_surface_t* m_surface; cairo_t* m_context; }; CairoRenderer::CairoRenderer() : m_pimpl(new CairoRendererImpl()) { ... } CairoRenderer::~CairoRenderer() { delete m_pimpl; } </code></pre> <p>The problem I have is when I try to access the m_pimpl member from a derived class, I get the compiler error:</p> <pre><code>error C2027: use of undefined type 'CairoRenderer::CairoRendererImpl' </code></pre> <p>Am I doing pimpl wrong? Or is what I want to do even possible?</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.
 

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