static data member

M

marcus

I have a class which has one static data member.
The .h file containing this class is included from many cpp files.
Therefor I have the definition part of the data member in the
implementation of the class (in the .cpp file) to avoid getting
"multiple defined..." error. In the .cpp file I put the definition
outside any method.

In the .h file:
class ....
{
private:
static GeometryDataLL *allMeshes;
}

In the .cpp file
GeometryDataLL* scGeometry::allMeshes = (GeometryDataLL*) calloc (1,
sizeof(GeometryDataLL));

On Windows ME on my laptop, this works fine (the data member allMeshes
is allocated before the constructor is run), but on my stationary
computer which has Windows XP, I get a crash in the constructor of the
..cpp file, because the allocation has not been done.

Why is this, and how can I avoid this.

thanks
Marcus
 
R

red floyd

marcus said:
I have a class which has one static data member.
The .h file containing this class is included from many cpp files.
Therefor I have the definition part of the data member in the
implementation of the class (in the .cpp file) to avoid getting
"multiple defined..." error. In the .cpp file I put the definition
outside any method.

In the .h file:
class ....
{
private:
static GeometryDataLL *allMeshes;
}

In the .cpp file
GeometryDataLL* scGeometry::allMeshes = (GeometryDataLL*) calloc (1,
sizeof(GeometryDataLL));

On Windows ME on my laptop, this works fine (the data member allMeshes
is allocated before the constructor is run), but on my stationary
computer which has Windows XP, I get a crash in the constructor of the
.cpp file, because the allocation has not been done.

Why is this, and how can I avoid this.

thanks
Marcus

First of all, do not use calloc(), use "new GeometryDataLL";

Second, is this a library? I've noticed issues with static class data
not getting initialized with VC7.1, especially when it's in a library.
 
J

John Harrison

marcus said:
I have a class which has one static data member.
The .h file containing this class is included from many cpp files.
Therefor I have the definition part of the data member in the
implementation of the class (in the .cpp file) to avoid getting
"multiple defined..." error. In the .cpp file I put the definition
outside any method.

In the .h file:
class ....
{
private:
static GeometryDataLL *allMeshes;
}

In the .cpp file
GeometryDataLL* scGeometry::allMeshes = (GeometryDataLL*) calloc (1,
sizeof(GeometryDataLL));

On Windows ME on my laptop, this works fine (the data member allMeshes
is allocated before the constructor is run), but on my stationary
computer which has Windows XP, I get a crash in the constructor of the
.cpp file, because the allocation has not been done.

Why is this, and how can I avoid this.

Probably you have run into this problem
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.11.

One way to avoid it would to be code something like this

class scGeometry
{
public:
scGeometry()
{
if (allMeshes == 0)
allMeshes = (GeometryDataLL*) calloc (1,
sizeof(GeometryDataLL));
...
}
private:
static GeometryDataLL *allMeshes;
}

GeometryDataLL* scGeometry::allMeshes = 0;

Incidentally why are you using calloc instead of new? It's dangerous to use
calloc to allocate an object, it will only work if GeometryDataLL is defined
a certain way. Use new and create a constructor to initialise
GeometryDataLL, its safer that way.

john
 
J

John Harrison


Here's a more complete example, you might be doing something like this

// a.h
class SomeClass
{
scGeometry geom;
};

// a.cpp
SomeClass a_global;

// b.h
class scGeometry
{
public:
scGeometry()
{
// some code that uses allMeshes
...
}
private:
static GeometryDataLL *allMeshes;
};

// b.cpp
GeometryDataLL* scGeometry::allMeshes = (GeometryDataLL*) calloc (1,
sizeof(GeometryDataLL));

For this code to work, the scGeometry::allMeshes in b.cpp must be
initialised before a_global in a.cpp and C++ provides no guarantee that this
will be the case.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top