lundi 2 janvier 2017

C++17 construct array in stack using choosen constructor (same constructor parameter values for each array entry)

Is it a way in c++17 to construct an array in stack using another constructor than the default constructor.

This is a special case when each array value is constructed with the same constructor parameters.

I need to use a basic stack located array (not a vector or an array of pointers or something else).

This code illustrate what I would like to do :

#include <iostream>
using namespace std;

struct A {
    A() {
        printf("A() called\n");
    }

    A(int i, int j) {
        printf("A(%d, %d) called\n", i, j);
    }
};

struct B {
    A tab[100];

    B(int i, int j) {
        // I would like to call A(i, j) for each tab entries instead of the default constructor
        printf("B(%d, %d) called\n", i, j);
    }
};

int main() {
    B b(1, 7);
    return 0;
}

Thanks

Aucun commentaire:

Enregistrer un commentaire