Re: Question about arrays in objects

From: Joec (joec_at_annuna.com)
Date: 01/17/04


Date: Sat, 17 Jan 2004 03:11:45 GMT


Leor Zolman wrote:

> On Sat, 17 Jan 2004 01:52:28 GMT, Joec <joec@annuna.com> wrote:
>
>
>>I want to create a 2d array as an object member.
>>
>>like:
>>
>>class x{
>>private:
>>const int num;
>>int array[num];
>>...
>>
>>do I have to use a pointer like:
>>int *px;
>>
>>then put the array in the constructor and use the pointer?
>
>
> A couple of things need clarification here:
>
> 1. You said "2d" array, but you show a definition for a 1d array (and
> have only a single dimension value, "num"). Do you really want a
> 1-dimensional, or a 2-dimensional array?
>
> 2. Do you intend for the dimension(s) of your array (be it 1d or 2d)
> to be "compile time constant", or do you want the dimension(s) to be
> supplied at run time at the time the object containing this array is
> to be instantiated? That will drive your implementation technique.
>
> Just to keep the examples simple, let's use a 1d array (although the
> pointer-based solution I'll discuss below gets more complicated with
> multiple dimensions). If the dimension is to be a compile-time
> constant (not what I think you're looking for, but I'm just trying to
> cover all the bases), then the best way to define it is as an
> enumeration constant:
>
> class x {
> enum {num = 100};
> int array[num][num];
> };
>
> There's at least one other way to do this, using a static const int
> (or size_t, rather, if you want to be a stickler about it), but I
> think the enum way is the cleanest.
>
> Now, if you want the size of the array to be specified at run-time and
> handled in the constructor, you just can't use a plain old array. A
> std::vector would be a good choice, because it'll handle its own
> memory management for you, and you wouldn't need any asterisks in your
> program (well, at least not for this).
>
> If you'd prefer to "roll your own" dynamic array implementation, then
> that is where using a pointer would come in. Note that you can't use a
> built-in array, because array dimensions must be compile-time
> constants. A class data member defined as
> const int num;
> the way you wrote it above wouldn't work because the compiler can't
> predict its value at compile time. num would have to be set in the
> constructor using a member initializer, e.g.,
> x::x(int size) : num(size) { ... }
> and thus it would not count as a "compile time constant".
>
> In your class, you'll need at least a pointer to int. Most folks
> would also store the dimension in the object, in order to permit
> bounds checking or size querying. The class definition would then have
> at least this:
>
> class x {
> public:
> x(size_t size); // constructor
> ~x(); // destructor
> // other member functions...
> private:
> size_t n_elements;
> int *datap;
> };
>
> and the job of the constructor would be to set n_elements to the value
> of the "size" parameter, and to allocate memory for holding that many
> ints and save a pointer to that memory in datap. Many folks prefer to
> do all that in the initializer list of the constructor, because there
> are often performance benefits in C++ to doing as much as you can in
> the initializer list rather than in the body of the constructor
> (though it wouldn't matter much in this case):
>
> x(size_t size) : n_elements(size),
> datap(new int[size])
> {}
>
> That should get you started... oh yeah, what about if you really
> wanted two dimensions? Unfortunately, this isn't as easy to generalize
> as the compile-time constant-sized array code would be. You might
> choose to just allocate a 1-dimentional array (sized by multiplying
> the x and y dimensions provided to the constructor). Another approach
> might be to define a 1d dynamic array of pointers to int (using a
> single pointer as above, but declared as int ** instead of just int
> *), and then allocate each int * in the array individually. This is a
> pain to set up, but accessing elements might go faster by allowing you
> to avoid multiplications to locate individual elements. Fun, fun.
> HTH,
> -leor
>
>
>
>
>
>
>
>
> Leor Zolman
> BD Software
> leor@bdsoft.com
> www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
> C++ users: Download BD Software's free STL Error Message
> Decryptor at www.bdsoft.com/tools/stlfilt.html

Wow it is pretty complex. Mainly I want a static array of objects as a
data member. I am no expert at C++ but I am trying to expand the
knowlege I have from several books. I am trying to apply what I have
learned. The more I study the more I realize how much many books leave
out and hard it is in choosing books.

Thanks for the reply and the pointers.



Relevant Pages

  • Re: Sorting records using sort()
    ... > We have an array of n*m bytes. ... > proxy objects store a pointer to some master descriptor that stores the ... > need a default constructor, the value type needs to be assignable only ...
    (comp.lang.cpp)
  • Re: C++ 101 dumb question
    ... Its action is to copy the data members of the class, ... If one of those members is a pointer, ... have it's own copy constructor would the default copy constructor call ... as a copy of the class being returned is placed on the stack for the ...
    (microsoft.public.vc.language)
  • Re: C++ 101 dumb question
    ... Yep I do comprehend the difference between a pointer and the data it ... If you have a CMyClass a; ... What does the signature of a copy constructor look like? ... Its action is to copy the data members of the class, ...
    (microsoft.public.vc.language)
  • Re: Function lookup tables?
    ... Ignore any typos the source is good except howto initialize the function ... > I want to initialize the function lookup table array to my member ... > function calls of the class inside of the constructor for the class. ... > What is the correct code to initialize the function pointer array ...
    (comp.lang.cpp)
  • Re: Youre appointed as Portability Advisor
    ... None of its behaviour is undefined by the Standard. ... to write to member A of a union and then read from member B, ... character type that treats the thing as an array of bytes. ... dereferencing a pointer to one ...
    (comp.lang.c)