J
josh
Hi, which is the way to init a static array inside a constructor
function?
i.e. if I had to init --->> int x[] = {100,200}
function?
i.e. if I had to init --->> int x[] = {100,200}
Hi, which is the way to init a static array inside a constructor
function?
i.e. if I had to init --->> int x[] = {100,200}
Hi, which is the way to init a static array inside a constructor
function?
i.e. if I had to init --->> int x[] = {100,200}
If the member is static it is not initialized in the constructor, but
outside the class declaration.
struct Foo
{
static int x[];
};
int Foo::x[] = {1,2};
so if I want in a class declaration to have an array like
int x[] not static I must make it as pointer and than in a
constructor init as:
class T
{
int *p;
T();
}
T::T()
{
p = new int[3];
p[0] = 100;
p[1] = 200;
p[2] = 300;
}
but this is so boring...
so if I want in a class declaration to have an array like
int x[] not static I must make it as pointer and than in a
constructor init as:
class T
{
int *p;
T();}
T::T()
{
p = new int[3];
p[0] = 100;
p[1] = 200;
p[2] = 300;
}
but this is so boring...
Why the pointer? You can do exactly the same thing with "int
array[ 3 ] ;". For initialization, there are two possibilities:
wrap it in a struct,
class T
{
struct A { int a[ 3 ] ; } ;
A data ;
T() ;
}
static T::A initData = {{ 100, 200, 300 }} ;
T::T()
: data( initData )
{
}
or use copy from a static array:
class T
{
int a[ 3 ] ;
T() ;
}
T::T()
{
static int const init[3] = { 100, 200, 300 } ;
std::copy( init, init+3, a ) ;
}
--
James Kanze (GABI Software) email:[email protected]
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
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.