[Please do not mail me a copy of your followup]
Andrey Karpov <
karpo...@gmail.com> spake the secret code
<
96e360cf-fb29-428c...@googlegroups.com> thusly:
I don't see the problem in the 5th place entry. The code excerpt
looks like this:
class G4PhysicsModelCatalog
{
private:
....
G4PhysicsModelCatalog();
....
static modelCatalog* catalog;
....
};
G4PhysicsModelCatalog::G4PhysicsModelCatalog()
{ if(!catalog) {
static modelCatalog catal;
catalog = &catal;
}
}
G4int G4PhysicsModelCatalog::Register(const G4String& name)
{
G4PhysicsModelCatalog();
....
}
Now there is something missing here to allow me to completely
understand this snippet out of context. We don't see the static
initializer for the catalog member. Presumably it looks like
modelCatalog *G4PhysicsModelCatalog::catalog = nullptr;
It seems the call to the constructor in the final function snippet is
intended to get the static member catalog initialized. You are
correct in that the code as written constructs a temporary object and
then destroys it, but it also invokes the constructor body which will
initialize this static member if it isn't already initialized.
What exactly is the problem here?
I agree with your recommendations for a better design that explicitly
reveals the intent (initialize the static members to a well known
state; again static class members are just another kind of global
variable). However, other than a clunky design, I'm not seeing any
bugs here.
--
"The Direct3D Graphics Pipeline" free book <
http://tinyurl.com/d3d-pipeline>
The Terminals Wiki <
http://terminals-wiki.org>
The Computer Graphics Museum <
http://computergraphicsmuseum.org>
Legalize Adulthood! (my blog) <
http://legalizeadulthood.wordpress.com>