new、delete、析构函数、自动类型转换

new

 分配内存,返回指针

new 类型名T (初值列表)

功能:申请用于存放T类型对象的内存空间,并依初值列表赋以初值

结果值:

成功->T类型的指针,指向新分配的内存

失败->0(NULL)

int *pl = new int;
int *pl = new int(10);

 

注意与malloc的区别

 malloc(m):开辟m字节长度的地址空间,并返回这段空间的首地址。

sizeof(x):计算变量x的长度。

free(p):释放指针p所指变量的存储空间,即彻底删除一个变量。

delete

 功能:释放指针P所指向的内存。P必须是new操作的返回值。

delete pl;

 

 

析构函数

  如果构造函数使用new来分配内存,则析构函数将使用delete来释放这些内存。如果构造函数没有使用new,析构函数实际上没有需要完成的任务。在这种情况下,只需要让编译器生成一个什么都不需要做的隐式析构函数即可。

 

  通常由编译器决定什么时候调用析构函数,代码中不会显式的调用析构函数(当然也会存在例外的情况)。如果创建的是静态存储对象,则其析构函数将在程序结束时自动调用。如果创建的是自动存储类对象,则其析构函数将在程序执行完代码块时自动被调用。如果是通过new创建的,则它将驻留在栈内存或自由存储区中,当使用delete来释放内存时,其析构函数将自动被调用。最后,程序可以创建临时对象来完成特定的操作,在这种情况下,程序将在结束对该对象的使用时自动调用其析构函数。

 

  由于在类对象过其实析构函数将被自动调用,因此必须有一个析构函数。如果程序员没有提供析构函数,编译器将隐式地声明一个默认析构函数,并在发现导致对象被删除的代码后,提供默认析构函数的定义。

#include <cstring>
#include <iostream>

using namespace std;

class Point {
    int x, y;

   public:
    // constructor
    Point(int _x = 0, int _y = 0);
    // deconstructor
    ~Point();
    // cout <<  overload
    friend ostream& operator<<(ostream& os, const Point& p);
};

Point::Point(int _x, int _y) {
    this->x = _x;
    this->y = _y;
    cout << "\nPoint is called!";
}

Point::~Point() { cout << "\n~Point is called!"; }

ostream& operator<<(ostream& os, const Point& p) {
    cout << "(" << p.x << "," << p.y << ")";
    return os;
}

template <typename T>
class DynamicArray {
   private:
    T* array;                 // pointer
    unsigned int mallocSize;  // the length of dynamic array

   public:
    // Constructors
    // mallocSize=length, and the new element is content
    DynamicArray(unsigned length, const T& content);

    // Destructors
    ~DynamicArray();

    // Copy Constructor
    DynamicArray(const DynamicArray<T>& anotherDA);

    // return the this->mallocSize
    unsigned int capacity() const;

    // for the array[i]=someT.
    T& operator[](unsigned int i);

    DynamicArray<T>& operator=(const DynamicArray<T>& anotherDA);
};

template <typename T>
DynamicArray<T>::DynamicArray(unsigned length, const T& content) {
    this->mallocSize = length;
    cout << endl
         << "new T[" << this->mallocSize << "] malloc " << this->mallocSize
         << "*" << sizeof(T) << "=" << this->mallocSize * sizeof(T)
         << " bytes memory in heap";
    this->array = new T[this->mallocSize];
    for (int i = 0; i < length; ++i) {
        this->array[i] = content;
    }
};

template <typename T>
DynamicArray<T>::~DynamicArray() {
    cout << endl
         << "delete[] array free " << this->mallocSize << "*" << sizeof(T)
         << "=" << this->mallocSize * sizeof(T) << " bytes memory in heap";
    delete[] array;
};

template <typename T>
DynamicArray<T>::DynamicArray(const DynamicArray<T>& anotherDA) {
    // this = anotherDA;
    cout << endl << "Copy Constructor is called";
    this->mallocSize = anotherDA.mallocSize;
    this->array = new T[this->mallocSize];
    for (int i = 0; i < this->mallocSize; ++i)
        this->array[i] = anotherDA.array[i];
};

template <typename T>
DynamicArray<T>& DynamicArray<T>::operator=(const DynamicArray<T>& anotherDA) {
    cout << endl << "operator = is called";
    if (this == &anotherDA) return *this;
    if (this->array) delete[] this->array;
    this->mallocSize = anotherDA.mallocSize;
    this->array = new T[this->mallocSize];
    for (int i = 0; i < this->mallocSize; ++i)
        this->array[i] = anotherDA.array[i];
    return *this;
}

template <typename T>
unsigned int DynamicArray<T>::capacity() const {
    return this->mallocSize;
}

template <typename T>
T& DynamicArray<T>::operator[](unsigned int i) {
    return this->array[i];
}

// StudybarCommentBegin
int main() {
    int length, i;
    cin >> length;

    DynamicArray<Point> iarray(length, Point(3));

    DynamicArray<Point> iarray2(iarray), iarray3(iarray2);

    cout << endl;
    for (i = 0; i < length; i++) {
        cout << iarray3[i] << " ";
        iarray[i] = Point(i, i + 1);
    }
    iarray3 = iarray2 = iarray;
    cout << endl;
    for (i = 0; i < length; i++) {
        cout << iarray3[i] << " ";
    }

    return 0;
}
// StudybarCommentEnd

 

posted @ 2021-03-10 11:53  Veritas_des_Liberty  阅读(224)  评论(0编辑  收藏  举报