Re: Question about arrays in objects

From: Jacques Labuschagne (jacques_at_clawshrimp.com)
Date: 01/17/04


Date: Sat, 17 Jan 2004 15:48:38 +1300

Joec 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?
>

The obvious solutions are

class x{
     int* array;
   public:
     explicit x(int N): array(new int[N]){}
     ~x(){ delete[] array; }
};

or

class x{
     int array[12]; // yuck
};

or

template<int N>
class x{
     int array[N];
};

HTH,
Jacques



Relevant Pages

  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: More a C++ Q then vs... but why not?
    ... You can't have an array of indeterminate size as a member. ... The pointer to "blah" is being stored in your object. ...
    (microsoft.public.vstudio.general)
  • Re: why cannot assign to function call
    ... hypothetical C-like languages, ... sizeof business would still indicate that a pointer was being passed. ... talk about variables of an array type. ... the earlier version of the standard didn't have numbered ...
    (comp.lang.python)
  • Re: multi dimensional arrays as one dimension array
    ... please - where does the standard say that such a conversion ... Pointer conversion yields a pointer to the same object as ... exist only where there are array declarations. ...
    (comp.lang.c)
  • Re: Evaluating unary *
    ... 'arr' exists, ... value can be used with the same syntax as would be used to access a 2D array of the kind you're referring to, but that 2D array is just a different way of looking as the same object that was already created by the definition of 'arr'. ... to me, it makes sense to return a pointer to the first value of an array, but to return the address of the pointer to the first value of an array, is not directly possible as such. ... lea eax, ...
    (comp.std.c)