static - C++: Singleton? How to pass arguments to the construtors? -
i reading item 47 in "effective c++". in book suggested called non-local static objects should used special care. instead suggests use below:
directory& tempdir() { static directory td; return td; }
i not sure if should called singleton. however, thinking how can pass arguments constructor of class directory. instance, want pass path string directory td, perhaps need this:
directory& tempdir(std::string & str) { static directory td(str); return td; }
the problem whenever want access directory td, have pass string input argument. not beautiful.
does have more elegant way of doing this?
thank you.
does have more elegant way of doing this?
instead of passing path in function, write global function "generates" path:
char const* get_path() { // do_stuff , return path } directory& tempdir() { static directory td(get_path()); return td; }
the better way not use singleton though. create object in main()
-function, initialize there , pass reference components.
you can of course have default parameter values
directory& tempdir(const std::string & str = "")
, if of use case.
that's nonsense. creating temporary every time call function unnecessary overhead.
Comments
Post a Comment