// (C) Porkolab 2003 // // A.4.6. // // Arrays #include <vector> struct X { X(int i) { x = i; }; int x; }; struct Y { int y; }; int main() { const int n = 4; int k = 4; // X x1[n]; // no default constructor X x2[n] = { 1, 2, 3, 4}; Y y1[n]; Y y2[k]; // k is non-const: error by standard // X *xp = new X[k]; // no default constructor Y *yp = new Y[k]; // std::vector<X> xv(10); // no default constructor std::vector<Y> yv(10); delete [] yp; } // // arrays are not polymorphic! // #include <iostream> using namespace std; struct Base { Base() { cout << "Base" << " "; } virtual ~Base() { cout << "~Base" << endl; } int i; }; struct Der : public Base { Der() { cout << "Der" << endl; } virtual ~Der() { cout << "~Der" << " "; } int it[10]; // sizeof(Base) != sizeof(Der) }; int main() { Base *bp = new Der; Base *bq = new Der[5]; delete bp; delete [] bq; // this causes runtime error }