On Dec 20, 5:38 pm, werasm <wer...@gmail.comwrote:
Quote:
On Dec 20, 11:02 pm, Markus Dehmann <markus.dehm...@gmail.comwrote:
>
Quote:
Does anyone have a hint how to do the forward decl so that I don't
have to recompile every class that includes the Opts decl whenever I
add an option?
>
Someone may have a solution, but I would try to give the generic base
a clear responsibility - singleton instantiation of the applicable
derived. If options are applicable to the derived only, then it should
not be part of the singleton. I suppose the reason you want to make
it part of the singleton is because you need to instantiate each
derived with options. A couple of questions then come to mind.
I think there is a misunderstanding. All I care about is that I can
use the Opts class anywhere (globally) in my code, using calls like
this:
// set a global value
Opts::Instance().option1 = 124;
// get a global value
if(Opts::Instance().option2){
// do something
}
That's the typical use for the singleton design pattern. I happened to
give code that implements a special Singleton class providing the
Instance() method, but I could have implemented this function directly
in the Opts class and not use inheritance.
The question is how to make calls like "Opts::Instance().option1"
possible, but with a design that allows for a forward declaration of
the class Opts, so that I can add or remove variables in the class
(like int option1, or bool option2) without having to recompile my
whole project (because many files will want to include the Opts
class.)
Quote:
- Is the type of the options unique per singleton/derived?
- If not unique, can the type of options vary.
- Can the value of options vary (is it default constructible or does
is only have static const members.
The values can vary. Any caller may set or get the public variables.
(I don't think getter and setter methods are necessary.)
Quote:
An idea might be to let the derived get its options from
a factory during its construction possible using typeid
or a string. I personally don't think it is idiomatic to
have arguments in instance. Otherwise you could possible
consider explicit creation of the singleton which
uses a non-instance method, the creation taking option
as argument.
Using a factory might be an idea ...
Quote:
In general the implementation of template code should be
stable as modification to it affects all translation units
that use it. If this is not the case, I think the unstable
code should be moved.
Yes, I don't expect the Singleton class to be modified during
development. Just the Opts class, whenever we add another option
variable (e.g. double option3;)
--Markus