Is this allowable?
class object
{
public:
struct st_example {
int i;
int j;
int c[d];
st_example(int i_i, J_j, d_d) : i(i_i), j(j_j), d(d_d) {}
};
object(int i_ii, int j_jj, int d_dd)
{
struct st_example test(i_ii, j_jj, d_dd);
}; // Constructor
};
So that:
object testObj(1,2,3);
What’s
dinst_example? I don’t see any variabled, soyou can’t initialize it, nor use it in an expression.
If you simply want
st_exampleto contain an array whose sizeis determined at runtime, use
std::vector. Trying to do it asyou do cannot be made to work; you define a local
st_example,and the compiler has to know how much space to allocate for it,
at compile time.
If all cases of
st_exampleare allocated dynamically, thereare some dirty tricks you can play:
(Formally, this is undefined behavior; The standard does not
guarantee that values set in the
operator newwill still bethere in the constructor. In practice, however, it will work.)
Client code has to allocate using
new (d) st_example(i, j),and access to elements of
cisc()[i]. And in the moregeneral case, you’ll likely have to worry about alignment.
(Here, everything is
int, so the alignment works outautomatically.)
I’d recommend sticking with
std::vector, however. It’s a lotsimpler, and a lot less fragile.