简体   繁体   English

C ++数组构造函数

[英]C++ Array Constructor

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. 据我所知,在C ++中无法在如上所述的Constructor中创建数组。 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. 数组元素由编译器通过其默认构造函数自动创建,然后将其手动分配给C'tor块中的特定值。 What is annoying; 烦人的事 for such a workaround, the class C has to offer a default Constructor. 为此,类C必须提供一个默认的构造方法。

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. 我知道使用std :: vector可能是一个解决方案,但是由于项目条件,我不允许使用任何标准的Boost或第三方库。

Arrays -- a concept older than C++ itself, inherited straight from C -- don't really have usable constructors, as you're basically noticing. 数组-比C ++本身更老的概念,直接继承自C-确实没有可用的构造函数,正如您基本上注意到的那样。 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). 有迹象表明,留给你给你提怪异的限制少的解决方法(无标准库?!?!?) -你可以有a是一个指针到C而不是C数组,分配原始内存吧,然后使用“ placement new”初始化每个成员(至少可以解决C没有默认构造函数的问题)。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM