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 two classes Using std::array
instead of the original array will solve your problem:
struct Base
{
int a;
std::arrayb;
};
struct Child: Base
{
Child(int aa, std:: arraybb): 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::arraybb): 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);
}< /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::arrayb;
};< br />
struct Child: Base
{
Child(int aa, std::arraybb): 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::arraybb): 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);
}
WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2834 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC