Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This line is particularly <em>bad</em>:</p> <pre><code>static library::Foo Foo; </code></pre> <p>It emits a <code>static</code> copy of <code>Foo</code> in every translation. Don't use it :) The result of <code>Foo::some_value</code> would be equal to the number of translations the Foo.h was visible to, and it's not thread safe (which will frustrate your users).</p> <p>This line will result in multiple definitions when linking:</p> <pre><code>int Foo::some_value = 0; </code></pre> <p><em>Singletons</em> are also bad. Searching here @SO will produce a lot of reasons to avoid them.</p> <p>Just create normal objects, and document to your users why they should share objects when using your library, and in which scenarios.</p> <blockquote> <p>User of the library doesn't need to worry about initialization. They wouldn't need to call something like Foo::init(); inside their main(), because library::Foo was initialized when my_project::Foo was constructed. This is the main design constraint here. User should not be responsible for initializing the library.</p> </blockquote> <p>Objects should be able to construct themselves as needed without introducing unstrippable binary baggage.</p> <blockquote> <p>I can create various private functions inside the library to control its use.</p> </blockquote> <p>That's not unique to your approach.</p> <blockquote> <p>The user can create other instances of this library if they choose, for whatever reason. But no copying would be allowed. One instance would be provided for them by default. This is a requirement.</p> </blockquote> <p>Then you can force your users to pass <code>Foo</code> as a necessary argument to create the types they depend upon (where Foo is needed).</p> <blockquote> <p>I can use the . syntax instead of ::. But that's a personal style thing.</p> </blockquote> <p>Not good. Not threadsafe, and the user can then seriously mess up your library's state. Private data is best.</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