学习心得
class MyArray
{
public:
explicit MyArray(int m_Capacity = 100);
explicit MyArray(const MyArray &array1);
~MyArray();
private:
int m_Capacity;
int m_Size;
int* p_Address;
};
MyArray::MyArray(int m_Capacity):m_Capacity(m_Capacity){
//this->m_Capacity = m_Capacity;
this->m_Size = 0;
this->p_Address = new int[this->m_Capacity];
printf("%s\n", __FUNCSIG__);
}
MyArray::MyArray(const MyArray& array1) {
//MyArray* tmp = new MyArray;
this->m_Size = array1.m_Size;
this->m_Capacity = array1.m_Capacity;
this->p_Address = new int[array1.m_Capacity];
memcpy(this->p_Address,array1.p_Address,array1.m_Capacity*sizeof(int));
}
MyArray::~MyArray()
{
if (this->p_Address)
{
delete[] this->p_Address;
}
this->m_Capacity = 100;
this->m_Size = 0;
printf("%s\n", __FUNCSIG__);
}
void test() {
//MyArray p; // 先MyArray::__autoclassinit2分配内存,直接调用 MyArray()默认构造
MyArray p1(100); // 先MyArray::__autoclassinit2分配内存,在调用MyArray(int m_Capacity = 100)有参构造函数,
MyArray p2(p1);// 先MyArray::__autoclassinit2分配内存,在调用MyArray::MyArray(const MyArray& array1) copy构造
printf("p1 :%x\n", &p1);
printf("p2 :%x\n", &p2);
}
所有类的都会自动生成xxxxx::__autoclassinit2,默认由__autoclassinit2去分配内存,在看情况调用自定义的构造函数。
上述缺少new,new出来的对象也一样;
笔记笔记!
网友评论