Re: Verification of initialization of global objects???

Igor.Smirnov_at_cern.ch
Date: 03/12/04


Date: Fri, 12 Mar 2004 16:19:46 +0100

On Fri, 12 Mar 2004, Rolf Magnus wrote:

> Igor.Smirnov@cern.ch wrote:
>
> > Hi,
> >
...
> > Thus the problem is finding the reliable, portable, fast and simple
> > way for a member function to determine whether the global object to
> > which it belongs has already been initialized or not yet.
>
> A non-static member function must never be called without a completely
> initialized object.

Funny, but I always did that and had no problems.

Anyway, you don't need that. This is a classic
> situation to which the solution is a singleton class.
>
> class MyClass
> {
> public:
> static MyClass& instance()
> {
> static MyClass inst;
> return inst;
> }
> private:
>
> // make instances of the class uncopyable
> MyClass(const MyClass&);
> MyClass& operator=(const MyClass&);
> };
>
> You can just write a default constructor now that does all the
> initialization. Just make it private. Later, you can use the singleton
> through the instance() function, like:
>
> MyClass::instance().doSomething();
>
>

Does this mean that there may be only one such object. Though now it does
not seem to be a problem for me, but it general?

But thank you for this idea! I guess it is portable for Linux, Windows
etc.

-- 


Relevant Pages

  • Re: Use of static and extern
    ... I'm not aware of any semantic difference between initialization of a const ... like a "normal" member function does. ... >the static qualifier limit scope to the file in which the definition ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Constructing an object
    ... > type Rectangle is new Shape with record ... If you want greater control of object creation and initialization, ... you could make the types private: ...
    (comp.lang.ada)
  • Re: Static-Initializer Failure Values...
    ... LeaveCriticalSection might fail due to defer initialization (init when ... throw-something member function init_and_lockwith postcondition ... nothrow member function unlockwith precondition "locked". ...
    (comp.programming.threads)
  • Re: Two questions about...something
    ... > I'm reading a STL book right now, ... > picked up the folowing syntax: ... The first one is a true initialization, ...
    (comp.lang.cpp)
  • Re: Collections & Events
    ... you can see that the same event handler is triggered when either the Options collection is modified OR any of the Options.Values data is modified and flips the boolean value of Options.OnChange to true. ... If I set a private boolean flag within each custom collection, when it comes time to check for the state, I'd have to do this: ... Since the ProductOptionValues collection has no intrinsic knowledge of the Options class, I can't set that flag from within it and am forced to use a more complicated check of the value. ... Since I'm adding products during initialization, I don't want the boolean "HasChanged" value to be set there, so I'm wiring up the event handlers after the collections are populated so I can detect any changes post-initializaton. ...
    (microsoft.public.dotnet.languages.csharp)