C ++ does not allow a class containing an array of elements that are not constructive by default:
class Gordian {
public:
int member;
Gordian(int must_have_variable) : member(must_have_variable) {}
};
class Knot {
Gordian* pointer_array[8];
Gordian inlined_array[8];
};
As even novice C ++ users know, the language ensures that all members that are not members of the POD are initialized when the class is built. And he does not trust the user to initialize everything in the constructor - you need to provide valid arguments to the constructors of all members before the constructor body even starts.
This is usually a great idea, as far as I know, but I came across a situation where it would be much easier if I could have an array of structural objects that are not standard.
: . , . ( ). , , , - .
, , , . , , :
class Knot {
public:
struct dummy { char padding[sizeof(Gordian)]; };
dummy inlined_array[8];
Gordian* get(int index) {
return reinterpret_cast<Gordian*>(&inlined_array[index]);
}
Knot() {
for (int x = 0; x != 8; x++) {
new (get(x)) Gordian(x*x);
}
}
};
, , ++. , . , :
1) , ? ? ( ++ 0x GCC).
2) ?