Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think what you are looking for is something called the "pimpl idiom". To understand how this works, you need to understand that in C++ you can forward declare something like so.</p> <pre><code>class CWidget; // Widget will exist sometime in the future CWidget* aWidget; // An address (integer) to something that // isn't defined *yet* // later on define CWidget to be something concrete class CWidget { // methods and such }; </code></pre> <p>So to forward declare means to promise to fully declare a type later. Its saying "there will be this thing called a CWidget, I promise. I'll tell you more about it later.".</p> <p>The rules of forward declaration say that you can define a pointer or a reference to something that has been forward declared. This is because pointers and references are really just addresses-a number where this yet-to-be-defined thing will be. Being able to declare a pointer to something without fully declaring it is convenient for a lot of reasons.</p> <p>Its useful here because you can use this to hide some of the internals of a class using the "pimpl" method. Pimpl means "pointer to implementation". So instead of "widget" you have a class that is the actual implementation. The class you are declaring in your header is just a pass-through to the CImpl class. Here's how it works:</p> <pre><code>// Thing.h class CThing { public: // CThings methods and constructors... CThing(); void DoSomething(); int GetSomething(); ~CThing(); private: // CThing store's a pointer to some implementation class to // be defined later class CImpl; // forward declaration to CImpl CImpl* m_pimpl; // pointer to my implementation }; </code></pre> <p>Thing.cpp has CThing's methods defined as pass-throughs to the impl:</p> <pre><code>// Fully define Impl class CThing::CImpl { private: // all variables public: // methods inlined CImpl() { // constructor } void DoSomething() { // actual code that does something } //etc for all methods }; // CThing methods are just pass-throughs CThing::CThing() : m_pimpl(new CThing::CImpl()); { } CThing::~CThing() { delete m_pimpl; } int CThing::GetSomething() { return m_pimpl-&gt;GetSomething(); } void CThing::DoSomething() { m_impl-&gt;DoSomething(); } </code></pre> <p>tada! You've hidden all the details in your cpp and your header file is a very tidy list of methods. Its a great thing. The only thing you might see different from the template above is that people may use boost::shared_ptr&lt;> or other smart pointer for the impl. Something that deletes itself.</p> <p>Also, keep in mind this method comes with some annoyances. Debugging can be a tad bit annoying (extra level of redirection to step through). Its also a lot of overhead for creating a class. If you do this for every class, you'll get tired of all the typing :). </p>
 

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