5

I was just wondering, whether an array member of a class could be created immediately upon class construction:

class C
{
      public:
          C(int a) : i(a) {}

      private:
          int i;
};

class D
{
 public:
        D() : a(5, 8) {}
        D(int m, int n) : a(m,n) {}

     private:
     C a[2];

};

As far as I have googled, array creation within Constructor such as above is in C++ impossible. Alternatively, the array member can be initialized within the Constructor block as follows.

class D
    {
     public:
         D() { 
               a[0] = 5;
               a[1] = 8; 
             }
         D(int m, int n) { 
                           a[0] = m;
                           a[1] = n; 
                         }
         private:
         C a[2];

    };

But then, It is not an array creation anymore, but array assignment. The array elemnts are created automatically by compiler via their default constructor and subsequently they are manually assigned to specific values within the C'tor block. What is annoying; for such a workaround, the class C has to offer a default Constructor.

Has anybody any idea which can help me in creating array members upon construction. I know that using std::vector might be a solution but, due to project conditions I am not allowed to use any standard, Boost or third party library.

1
  • 1
    You have to wait for C++0x, which allows this one. Commented Dec 22, 2009 at 7:40

2 Answers 2

5

Arrays -- a concept older than C++ itself, inherited straight from C -- don't really have usable constructors, as you're basically noticing. There are few workaround that are left to you given the weird constraints you're mentioning (no standard library?!?!?) -- you could have a be a pointer to C rather than an array of C, allocate raw memory to it, then initialize each member with "placement new" (that works around the issue of C not having a default constructor, at least).

Sign up to request clarification or add additional context in comments.

1 Comment

C arrays are often superior to vector when the size is fixed; C++ vectors also have no "construct from inline data" feature (although C++0x does, as mentioned, and you can declare a constant array and initialize from iterators).
1

You can create a class to wrap an array and construct as you like. Here's a start; this code is untested aside from what you see.

#include <iostream>
using namespace std;

template< class T, int N >
struct constructed_array {
        char storage[ sizeof( T[N] ) ]; // careful about alignment
        template< class I >
        constructed_array( I first ) {
                for ( int i = 0; i < N; ++ i, ++ first ) {
                        new( &get()[i] ) T( *first );
                }
        }
        T *get() const { return reinterpret_cast< T const* >( storage ); }
        T *get() { return reinterpret_cast< T * >( storage ); }
        operator T *() const { return get(); }
        operator T *() { return get(); }
};

char const *message[] = { "hello", ", ", "world!" };

int main( int argc, char ** argv ) {
        constructed_array< string, 3 > a( message );
        for ( int i = 0; i < 3; ++ i ) {
                cerr << a[i];
        }
        cerr << endl;
        return 0;
}

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.