array init in constructor

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}
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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};
 
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}

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...
 
J

James Kanze

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 ) ;
}
 
J

josh

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

why always static?
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top