473,327 Members | 2,074 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,327 software developers and data experts.

static array initialization

Hello,

I really don't understand the simplest thing at the moment. I have a static
array of pointers to a class . How do we initialize it?

class classA
{
public:
int x1;
};

class classB
{
public:
void foo() { cout<< m_ptr[0]->x1; }
static classA* m_ptr[4];
};

classA* classB::m_ptr[0]=0; // This is not correct !!!
classA* classB::m_ptr[1]=0; // This is not correct !!!
classA* classB::m_ptr[2]=0; // This is not correct !!!
classA* classB::m_ptr[3]=0; // This is not correct !!!

int main()
{
classB* ptr = new classB;

ptr->foo();

return 0;
}

Any idea? If "m_ptr" was not an array of pointers I would had initialize it
like this: "classA* classB::m_ptr=0;"
What about arrays now?
Jul 19 '05 #1
2 14079

"Makis Papapanagiotou" <Se********************@siemens.com> wrote in message
news:bj**********@news.mch.sbs.de...
Hello,

I really don't understand the simplest thing at the moment. I have a static array of pointers to a class . How do we initialize it?

class classA
{
public:
int x1;
};

class classB
{
public:
void foo() { cout<< m_ptr[0]->x1; }
static classA* m_ptr[4];
};

classA* classB::m_ptr[0]=0; // This is not correct !!!
classA* classB::m_ptr[1]=0; // This is not correct !!!
classA* classB::m_ptr[2]=0; // This is not correct !!!
classA* classB::m_ptr[3]=0; // This is not correct !!!

int main()
{
classB* ptr = new classB;

ptr->foo();

return 0;
}

Any idea? If "m_ptr" was not an array of pointers I would had initialize it like this: "classA* classB::m_ptr=0;"
What about arrays now?


Same way you initialise any array

classA* classB::m_ptr[4] = { 0, 0, 0, 0 };

but since zero initialisation is the default, you don't need to initialise
it at all.

classA* classB::m_ptr[4];

john
Jul 19 '05 #2

"Makis Papapanagiotou" <Se********************@siemens.com> wrote in message
news:bj**********@news.mch.sbs.de...
Hello,

I really don't understand the simplest thing at the moment. I have a static array of pointers to a class . How do we initialize it?

class classA
{
public:
int x1;
};

class classB
{
public:
void foo() { cout<< m_ptr[0]->x1; }
static classA* m_ptr[4];
};

classA* classB::m_ptr[0]=0; // This is not correct !!!
classA* classB::m_ptr[1]=0; // This is not correct !!!
classA* classB::m_ptr[2]=0; // This is not correct !!!
classA* classB::m_ptr[3]=0; // This is not correct !!!


Try this -
classA* classB::m_ptr[]={0};

Jul 19 '05 #3

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

Similar topics

4
by: junger | last post by:
If a member object is static, is it possible to initialize it with function calls? E.g., if I want my class to contain a static vector filled with other static objects, can I fill this vector...
5
by: Jim Langston | last post by:
What I want to do: have a vector of ints in my class initialized with 0 to 499 which will later be pushed/popped out of the vector by instances. What I have: class CParticleStream // Yes, I...
1
by: Piotr Sawuk | last post by:
just a quick question out of curiosity: how to initialize const arrays? struct srat { long num; ulong den; srat(){} } struct collisions
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
15
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in...
1
by: bvisscher | last post by:
I posted this recently in microsoft.public.vc.language and was redirected here. I also searched this ng and found some relavant threads. The most relavent I found was: ...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
6
by: mathieu | last post by:
Hi there I am trying to provide a lookup from two 'int's into a char array, something like this: template <int g, int estruct Lookup; template <struct Lookup<0,0{ // some typedef + enums...
17
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
2
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.