1

I don't know (if it's even possible) how to allocate an array from a class that has constructor with parameters using another constructor with parameters. For example (I'll write just what's actually relevant. Uhm, I doubt the template is, but still):

template <typename T>
class C1 {
    T *array; 
    public:
        C1 (int n) {
            array = new T [n];
        };
};

class C2 {
    int length;
    int lengthArrayC1;
    C1<T> *array;
    public:
        C2 (int x, int y) {
            length = x;
            lengthArrayC1 = y;
            array = //and here's where I'm lost
        };
};

I tried writing something like this in many ways:

array = new [length] C1<T> (lengthArrayC1);

but none worked.

  • 1
    Not directly relevant to your question, but avoid using *int*. In general, *size_t* is the preferred type for such things. – luis.espinal Mar 10 '14 at 19:03
  • @luis.espinal IMHO that's seriously bad advice. First, it's recommending a type that invites bugs due to inadvertent invocation of modulo arithmetic (mostly due to implicit conversion for mixed type expressions). Secondly, very little code scales up to the regime where `int` doesn't suffice for a size, so it gives a false sense of security. Third, it's more to type. If one absolutely has to support gargantuan unrealistic arrays, then use the signed counterpart to `size_t`, namely `ptrdiff_t`. – Cheers and hth. - Alf Mar 10 '14 at 20:15
  • @Cheersandhth.-Alf - I'll be honest to say it is the first time I've ever heard such advice. Everywhere I've worked with C/C++ (app or embedded) we have followed closely the C-Cert advice as well as what, up to now, have appeared to be common wisdom of preferring size_t over plain signed int. – luis.espinal Mar 10 '14 at 20:41

2 Answers2

2

Can't do it: Object array initialization without default constructor

A type must have a no-argument constructor in order for you to new[] an array of it. But, see the tip in the answer to that question about using std::vector, which allows you to specify a prototype object to use for initializing the array elements.

Community
  • 1
  • 1
TypeIA
  • 16,916
  • 1
  • 38
  • 52
1

** Caveat, I don't have a C++ compiler at this moment, so there might be some syntax errors in my response. With that said, the idea still holds. **

Change your C1 so that it uses partial template specialization (note the second specialized argument)...

template <typename T, size_t default_size>
class C1 {
    T *array; 
    public:
        C1 (int n = default_size) {
            array = new T [n];
        };
};

... then you can do the following:

array = new [length] C1<T, lengthArrayC1>;

If you cannot modify C1 (because the code is owned by someone else... it happens), then you could do the following:

template <typename T, size_t default_size>
class MyC : public C1
{
public:
   MyC() : C1(default_size){}
};

...

array = new [length] MyC<lengthArrayC1>;

Making a new class just to do that is ugly, but sometimes it is the only way to get things going if we really have a need to the thing you are trying to do in your original question.

Hope it helps.

luis.espinal
  • 10,331
  • 6
  • 39
  • 55
  • I tried adding the constructor's parameter to the template, but for what I'm doing I get to know it at run time, and I need it at compile time. Thank you for answering. –  Mar 10 '14 at 19:36