On Apr 10, 1:21 pm, "josh" <xdevel1...@gmail.comwrote:
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:ja*********@gmail.com
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