473,411 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

array init in constructor

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}

Apr 10 '07 #1
4 6982
On 10 Apr, 11:30, "josh" <xdevel1...@gmail.comwrote:
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};

--
Erik Wikström
Apr 10 '07 #2
On 10 Apr, 13:07, "Erik Wikström" <eri...@student.chalmers.sewrote:
On 10 Apr, 11:30, "josh" <xdevel1...@gmail.comwrote:
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};

--
Erik Wikström
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...

Apr 10 '07 #3
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

Apr 10 '07 #4
On 10 Apr, 18:56, "James Kanze" <james.ka...@gmail.comwrote:
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:james.ka...@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
why always static?

Apr 11 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Servé Laurijssen | last post by:
Is it possible for an array to be initialised like this: int arr = { 0..9 }; So that the array holds 0 to 9 after that?
1
by: jimbo | last post by:
Here is my problem. I'm creating an Instrumentation class that will use previously created Performance Categories and Counters in order to time various processes (ie. query duration etc.). This...
1
by: alexkcha | last post by:
I was wondering if anyone can help me with this problem. #include <iostream> #include <stdlib.h> I am having problems compiling the following code. Can someone explain what is wrong with the...
4
by: Marcus Kwok | last post by:
I found a bug in VC++ .NET 2003 regarding default initialization of arrays of primitives in a constructor initialization list, as detailed in this post:...
10
by: mattias.k.nyberg | last post by:
So Im trying to learn to program with C#. And I have this question about why the string array won't work in the first class but it does in the second. To me it looks like they do the exact same...
4
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: ...
1
by: stn2007 | last post by:
#define VECTOR_LENGTH 255 #define MAX_RAD 15 #define MAX_ANGLE 15 double FR; double FI; condition a
4
by: Saile | last post by:
I'm creating an array inside the constructor of my class. Looks like: class SPR_ANIM { public: int index; SPR_ANIM::SPR_ANIM(int num); void Draw(); }; SPR_ANIM::SPR_ANIM(int...
6
by: npankey | last post by:
I've started experimenting with template metaprogramming in a small project of mine. What I'm trying to accomplish is to generate a static array of templated objects that get specialized based on...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.