Array of class and a pointer to it

G

Guest

Hello,

I defined a class name MyClass.

I would like to define an array of 4 by 8 of that class.
I would like to define a pointer to the array of MyClass.

I tried it with static definition:
MyClass m_MyClass[4][8] ;
I can not seem to find the way to define a pointer to this array?!?!?

I tried also to define it with the new operator but had similar results.

Any ideas how to do it?

Eitan
 
B

Boni

How about
MyClass ** ppMyClass;
ppMyClass=m_MyClass; or the same
ppMyClass=&m_MyClass[0][0];
or dynamically
MyClass ** ppMyClass=new MyClass *[4]
for (int i=0;i<8;++i){
ppMyClass=new MyClass [8]
}

Don't forget to do the same when you delete (not just delete[]ppMyClass)
Hope it helps,
Boni
 
B

Brian Muth

Boni said:
How about
MyClass ** ppMyClass;
ppMyClass=m_MyClass; or the same

No, the above line is invalid.

ppMyClass=&m_MyClass[0][0];

and this line is also invalid.

or dynamically
MyClass ** ppMyClass=new MyClass *[4]
for (int i=0;i<8;++i){
ppMyClass=new MyClass [8]
}


Ah, but that's not the same, is it? You have created an array of pointers,
not a two-dimensional array, which is what the OP wants.

Brian
 
B

Boni

No, the above line is invalid.
and this line is also invalid.
Hi Brian,
thanks to pointing that out. I am sorry for that advice.
Ah, but that's not the same, is it? You have created an array of pointers,
not a two-dimensional array, which is what the OP wants.
I think you are wrong on that. There is physically no such thing as a 2
dimentional array in the memory. It is array of pointers to pointers. The
first operator[] take the pointer to the array of values the second
operator[] takes the value.
With best regards,
Boni
 

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

Top