Topic: About initialize of the class static member variable.
Author: "madmanahong" <madmanahong@163.com>
Date: Tue, 11 Apr 2006 10:41:55 CST Raw View
I have a question about initialize of the class static member variable.
I Have a static member variable like this:
// A.h
class A
{
static int m_nStaticVar[1000];
};
If I want to initialize this static array to ZERO.
I can do this.
// A.cpp
int A::m_nStaticVar = { 0 };
the compiler can put the variable to .data section and set the array to
ZERO.
but now I don't want to initialize ZERO, I want to initialize 0x999.
How to do ?
--------------
I only knew a way like this;
class A
{
staitc int m_na[1000];
static bool init( );
static bool m_bInit;
};
int A::m_na[1000];
bool A::m_bInit = A::init();
bool A::init()
{
for ( int i=0;i<1000;++i )
{
A::m_na[i] = 0x999;
}
}
Can you give me a better way ??
thank you very much.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Tom s" <NULL@NULL.NULL>
Date: 11 Apr 2006 16:40:01 GMT Raw View
> Can you give me a better way ??
I'm just working off the cuff here, but I'd probably do something like this:
template<class T>
class InitValue
{
template<T value>
class Value
{
public:
T object;
Value() : object(value) { }
};
};
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main()
{
InitValue<int>::Value<999> array[1000];
for( int i = 0; i < 1000; ++i )
{
cout << array[i].object << endl;
}
}
You can do all sorts of fancy stuff like make an "operator T" for
"InitValue", but the above code does the trick.
-Tom s
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "kanze" <kanze@gabi-soft.fr>
Date: Wed, 12 Apr 2006 10:06:01 CST Raw View
madmanahong wrote:
> I have a question about initialize of the class static member variable.
> I Have a static member variable like this:
> // A.h
> class A
> {
> static int m_nStaticVar[1000];
> };
> If I want to initialize this static array to ZERO.
> I can do this.
> // A.cpp
> int A::m_nStaticVar = { 0 };
> the compiler can put the variable to .data section and set the
> array to ZERO.
> but now I don't want to initialize ZERO, I want to initialize 0x999.
> How to do ?
What's wrong with the obvious solution:
int A::m_nStaticVar = {
0x999, 0x999, 0x999, 0x999, 0x999 ...
} ;
Obviously, you don't want to have to type it in manually, but it
should be quick work to write a program to generate it. Or even
with macros:
#define Times5( n ) n, n, n, n, n,
#define Times10( n ) Times5( n ) Times5( n )
#define Times100( n ) Times10( Times10( n ) )
#define Times1000( n ) Times100( Times10( n ) )
int A::m_nStaticVar = {
Times1000( 0x999 )
} ;
Of course, if you use an std::vector, it's even easier. I'd say
that this is the preferred solution, unless order of
construction issues are involved.
--
James Kanze GABI Software
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]