T
Thunder
How can I declare a array as bool slice[x][y][z];
I want declare this in a class member.
Thanks.
I want declare this in a class member.
Thanks.
Thunder said:How can I declare a array as bool slice[x][y][z];
I want declare this in a class member.
Thunder said:How can I declare a array as bool slice[x][y][z];
I want declare this in a class member.
Thanks.
Thunder said:How can I declare a array as bool slice[x][y][z];
I want declare this in a class member.
Thanks.
What are x,y,z? Give some detail as to what you want to do. Have you
tried something? What problems did you have? Is this supposed to be a
dynamic array?
Thunder said:Default User ha scritto:
Thunder said:How can I declare a array as bool slice[x][y][z];
I want declare this in a class member.
Thanks.
What are x,y,z? Give some detail as to what you want to do. Have you
tried something? What problems did you have? Is this supposed to be
a dynamic array?
x,y,z are parameters of function member.Yes It's a dynamic array.
I want to do something as this.
void func()
{
int sx=....
int sy=....
int sz=....
slice=new [sx] [sy] [sz];
}
My problem is the following I don't know as I should declare array
slice in member class,
and I don't know as allocate memory for this array.
Thunder said:Default User ha scritto:Thunder wrote:
How can I declare a array as bool slice[x][y][z];
What are x,y,z?
x,y,z are parameters of function member.Yes It's a dynamic array.
I want to do something as this.
void func(){
int sx=....
int sy=....
int sz=....
slice=new [sx] [sy] [sz];
}
Well, you can't do that. See the FAQs:
<http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.16>
You could do it like this:
int ***slice = new int**[sx];
for(int i = 0; i < sx; i++){
slice = new int*[sy];
for(int j = 0; j < sy; j++)
slice[j] = new int[sz];
}
I recommend nesting std::vectors. Easier and less prone to problems.
I'd also look hard at the design that requires a three-dimensional
array.
How can I declare a array as bool slice[x][y][z];
Arr3D(size_t const a,size_t const b,size_t const c)
: s1(b*c),s2(c),p(new ArrElemT[a*b*c]) {}
Frederick said:Thunder:
How can I declare a array as bool slice[x][y][z];
I've seen elsethread that you're talking about dynamic allocation. You
could try something like the following:
#include <cstddef>
template<class ArrElemT>
class Arr3D {
private:
typedef std::size_t size_t;
size_t const s1,s2;
ArrElemT *const p;
public:
Arr3D(size_t const a,size_t const b,size_t const c)
: s1(b*c),s2(c),p(new ArrElemT[a*b*c]) {}
~Arr3D() { delete [] p; }
ArrElemT &GetElem(size_t const a,size_t const b,
size_t const c)
{
return *(p + a*s1 + b*s2 + c);
}
};
Frederick said:Frederick Gotham:
Arr3D(size_t const a,size_t const b,size_t const c)
: s1(b*c),s2(c),p(new ArrElemT[a*b*c]) {}
That would be better as:
new ArrElemT{a*s1)
Frederick said:(, not {.
...
I think it is clearer as you had it originally.
That way you'd make your code to depend critically on the order of class
data member declaration. If someone accidentally (or intentionally)
changed the order to
...
ArrElemT *const p;
size_t const s1,s2;
...
the initialization would fail. That's one thing to remember about
initialization lists.
Frederick said:Why is it determined by declaration order rather than initialition list
order? The latter would seem to make _a lot_ more sense.
Frederick said:...
Why is it determined by declaration order rather than initialition list
order? The latter would seem to make _a lot_ more sense.
...
Frederick said:Gavin Deane:
Yes, I would agree that it is clearer, but efficiency takes precedence for me
99% of the time (i.e. the code only evaluates "b*c" once rather than twice).
If C++ allowed "custom" subobject construction order (determined by
constructor initialization list, for example), compilers would have to
memorize that order at run-time for each object somehow, and then use it
later in each object's destructor in order to satisfy the
reverse-order-destruction requirement.
For the benefit of the OP, if you are programming in a collaborative
environment (ie working with other programmers on the same project),
professionally or otherwise, your fellow programmers will not, in
general, thank you for sacrificing readability in this kind of way.
Frederick said:Gavin Deane:
For the benefit of the OP, if you are programming in a collaborative
environment (ie working with other programmers on the same project),
professionally or otherwise, your fellow programmers will not, in
general, thank you for sacrificing readability in this kind of way.
If they're that heart broken, we can give them comments:
new ArrElemT[a*s1] /* s1 == b*c */
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.