Inheritance: Constructor, initialization C, similar to array members of the foundation class in C 11

Consider the following code:

struct Base //in my real scenario Base class can not be changed
{
int a;
double b[10];
};

struct Child: Base
{
Child(int aa, double bb[10]): Base{aa} {} //This works fine
Child(int aa, double bb[10]): Base{aa, bb} {} //this is not working
};

The child’s second constructor does not work. I get the error “Array must be initialized with an initializer enclosed in parentheses”.
How to initialize without changing the Base class The b in Child (for example, using vector instead of c-like array, I don’t allow this)

In Child’s constructor, bb is not an array: because decay, it is just a pointer. And you cannot initialize an array with a pointer (b is in Base).

In two classes Using std::array instead of the original array will solve your problem:

struct Base
{
int a;
std::array b;
};

struct Child: Base
{
Child(int aa, std:: array bb): Base(aa, bb) {}
};

However, since you mentioned that Base cannot be modified, you must manually copy the elements (in general , You can also use move, but the basic type is meaningless):

#include 
#in clude
#include

struct Base {
int a;
double b[10];
};

struct Child: Base {
Child(int aa, std::array bb): Base{aa} {
std::copy(bb.begin() , bb.end(), b);
}
};

int main() {
auto child = Child(3, {2, 3, 4});

for (auto it: child.b) {
std::cout << it << "";
}
}

Live on Coliru

You can also use references instead of std::array to achieve, but the syntax is a bit complicated:

Child( int aa, double const (&bb)[10]): Base{aa} {
std::copy(&bb[0], &bb[10], b);
}

< /div>

Consider the following code:

struct Base //in my real scenario Base class can not be changed
{
int a;
double b[10];
};

struct Child: Base
{
Child(int aa, double bb[10]): Base{aa} {} //This works fine
Child(int aa, double bb[10]): Base{aa, bb} {} //this is not working
};

The child's second constructor does not work. I get the error "Array must be initialized with a bracket-enclosed initializer".
How to initialize b in Child without changing the Base class (e.g. using vector instead of c-like array, I don’t allow it)

In Child’s constructor, bb is not an array: because decay, it’s just a pointer. And you You cannot initialize an array with a pointer (b is in Base).

Using std::array in both classes instead of the original array will solve your problem:

struct Base
{
int a;
std::array b;
};< br />
struct Child: Base
{
Child(int aa, std::array bb): Base{aa, bb} {}
} ;

However, since you mentioned that Base cannot be modified, you must manually copy the elements (in general, you can also use move, but the basic type does not make sense):

< /p>

#include 
#include
#include

struct Base {
int a;
double b[10];
};

struct Child: Base {
Child(int aa, std::array bb): Base{aa } {
std::copy(bb.begin(), bb.end(), b);
}
};

int main() {
auto child = Child(3, {2, 3, 4});

for (auto it: child.b) {
std::cout << it < <"";
}
}< /pre>

Live on Coliru

You can also use references instead of std::array to achieve, but the syntax is a bit complicated:

Child (int aa, double const (&bb)[10]): Base{aa} {
std::copy(&bb[0], &bb[10], b);
}

Leave a Comment

Your email address will not be published.