3.4 Объекты и классы

  • Published on
    08-Feb-2017

  • View
    343

  • Download
    0

Embed Size (px)

Transcript

  • C++ 3

    1/5

  • , .

    () .

    struct IntArray {explicit IntArray(size_t size);~IntArray ();int & get(size_t i);

    size_t size;int * data;

    };

    IntArray a(10);IntArray b = {20, new int [20]}; //

    2/5

  • new, .

    struct IntArray {explicit IntArray(size_t size);~IntArray ();

    size_t size;int * data;

    };

    // IntArray * pa = new IntArray (10);// IntArray * pb =

    (IntArray *) malloc(sizeof(IntArray )); 3/5

  • delete .

    // IntArray * pa = new IntArray (10);

    // delete pa;

    new [] delete []

    // 10 // ( )IntArray * pa = new IntArray [10];

    // delete [] pa;

    4/5

  • Placement new

    // void * p = myalloc(sizeof(IntArray ));

    // pIntArray * a = new (p) IntArray (10);

    // a->~IntArray ();

    // myfree(p);

    :

    char b[sizeof(IntArray )];new (b) IntArray (20); //

    5/5

Recommended

View more >