dynamic array of a nested class with an array in it? Can it be done? -
this code compiles crashes instantly. i've tried on devcppportable
. trying make class can store lot of complex data (i.e. object has multiple property sets , each set carries multiple figures).
each object created have unique number of property sets , internal property values. able shape class upon declaration not allocate bunch of unused space. possible?
#include<iostream> using namespace std; class { public: int amount; struct b { int max; int* prop; b() {} void set(int&); ~b(){delete prop;} }; b* property; a(int amt, int max0, int max1=0, int max2=0); ~a(){delete property;} }; int main() { object(2, 3, 5); return 0; } a::a(int amt, int max0, int max1, int max2) { amount = amt; property = new b[amt]; switch(amt) { case 3: property[2].set(max2); case 2: property[1].set(max1); case 1: property[0].set(max0); } } void a::b::set(int& m) {max = m; prop = new int[max];}
you allocate property
new[]
deleting delete
, not delete[]
Comments
Post a Comment