Topic: Constructor Parameters for Arrays


Author: fraley@nsa.hp.com (Bob Fraley)
Date: Sat, 19 Feb 1994 02:23:41 GMT
Raw View
Dag Bruck (dag@control.lth.se) wrote:
: >>>>> On Wed, 9 Feb 1994 00:04:27 GMT, fraley@nsa.hp.com (Bob Fraley) said:
: Bob> The C++ standard apparently precludes passing parameters to
: Bob> constructors for arrays of class type, either in a declaration or an
: Bob> invocation of new.

: No it doesn't.  See Stroustrup's The C++ Programming Language, 2nd
: edition, Section r.12.6.1:

:  complex v[6] = {1,complex(1,2),complex(),2 }

: -- Dag Bruck

Sorry, I didn't fully state the problem.

I'd like to declare an array of objects whose size is set by a const.
I don't want to change the initialization statement when the constant
is changed.  The initialization of the form shown above requires that
the  correct number of elements be coded in the initializer.    That
was the reason for ruling out the use of a static declaration.

So, I'm willing to call new to create the array.  That's where the
standard has stated that parameters cannot be passed to the constructor:

5.3.3   New        paragraph 8:
No initializers can be specified for arrays.  Arrays of objects of a
class with constructors can be created by a new-expression only if the
class has a default constructor.  In that case, the default
constructor will be called for each element of the array.


So, now with a better statement of the problem:

1.  Why can't constructor parameters be provided when doing a new of
an array of objects?

2.  Is there some way to provide a declaration which initializes an
array where the size is give by a const, and not requireing changes to
the initializer?

Thanks.

Bob Fraley






Author: g2devi@cdf.toronto.edu (Robert N. Deviasse)
Date: Sat, 19 Feb 1994 04:15:10 GMT
Raw View
In article <CLG9BI.FxL@nsa.hp.com> fraley@nsa.hp.com (Bob Fraley) writes:
>Dag Bruck (dag@control.lth.se) wrote:
>: >>>>> On Wed, 9 Feb 1994 00:04:27 GMT, fraley@nsa.hp.com (Bob Fraley) said:
>: Bob> The C++ standard apparently precludes passing parameters to
>: Bob> constructors for arrays of class type, either in a declaration or an
>: Bob> invocation of new.
>
>: No it doesn't.  See Stroustrup's The C++ Programming Language, 2nd
>: edition, Section r.12.6.1:
>
>:  complex v[6] = {1,complex(1,2),complex(),2 }
>
>: -- Dag Bruck
>
>Sorry, I didn't fully state the problem.
>
>I'd like to declare an array of objects whose size is set by a const.
>I don't want to change the initialization statement when the constant
>is changed.  The initialization of the form shown above requires that
>the  correct number of elements be coded in the initializer.    That
>was the reason for ruling out the use of a static declaration.
>
>So, I'm willing to call new to create the array.  That's where the
>standard has stated that parameters cannot be passed to the constructor:
>
>5.3.3   New        paragraph 8:
>No initializers can be specified for arrays.  Arrays of objects of a
>class with constructors can be created by a new-expression only if the
>class has a default constructor.  In that case, the default
>constructor will be called for each element of the array.
>
>
>So, now with a better statement of the problem:
>
>1.  Why can't constructor parameters be provided when doing a new of
>an array of objects?
>
>2.  Is there some way to provide a declaration which initializes an
>array where the size is give by a const, and not requireing changes to
>the initializer?
>


Yes, I've found the following technique quite handy:

   template<class T>
     struct VA_ARGS {
        T* arr;
        size_t size;

        VA_ARGS() : arr(0), size(0) {}
        VA_ARGS(const T& a1) : arr(new T[1]), size(0) { arr[0]=a1; }
        VA_ARGS(const T& a1,const T& a2) : arr(new T[2]), size(0) { arr[0]=a1; arr[1]=a2; }
        // ...
        // In my definition I go up to 10 args, which is good enough for most purposes.

        ~VA_ARGS() { delete arr; }
     };

This allows me to write:
     void print(VA_ARGS<int> p) {
        while(p.size--)
          cout << *++a.arr << ` `;
     }

     void test(){    print(VA_ARGS<int>(1,2,3,4,5);    }

and also

     VA_ARG<Complex> ca(2.0, Complex(2,3));

Of course, my implementation has some fixed limit on the number of constructor
arguments, but that doesn't seem to be a problem for me since whenever I need
a larger number, the compiler warns me to add another constructor.


>Thanks.
>
>Bob Fraley
>
>


Take care
    Robert
--
/----------------------------------+------------------------------------------\
| Robert N. Deviasse               |"If we have to re-invent the wheel,       |
| EMAIL: g2devi@cdf.utoronto.ca    |  can we at least make it round this time"|
+----------------------------------+------------------------------------------/




Author: fraley@nsa.hp.com (Bob Fraley)
Date: Wed, 9 Feb 1994 00:04:27 GMT
Raw View
The C++ standard apparently precludes passing parameters to
constructors for arrays of class type, either in a declaration or an
invocation of new.

What is the reason for this restriction?  It would seem to be easy
enough to pass the parameters when the constructor is called on an
element.

Bob Fraley
HP






Author: dag@control.lth.se (Dag Bruck)
Date: 09 Feb 1994 07:13:47 GMT
Raw View
>>>>> On Wed, 9 Feb 1994 00:04:27 GMT, fraley@nsa.hp.com (Bob Fraley) said:
Bob> The C++ standard apparently precludes passing parameters to
Bob> constructors for arrays of class type, either in a declaration or an
Bob> invocation of new.

No it doesn't.  See Stroustrup's The C++ Programming Language, 2nd
edition, Section r.12.6.1:

 complex v[6] = {1,complex(1,2),complex(),2 }

-- Dag Bruck