在下面的代码中,我有一个具有int的动态数组的类A. 我有另一个类B有一个指向类AI的对象的指针数组已经写入类A的拷贝构造函数。我需要为B类写一个拷贝构造函数和析构函数,我尝试了各种方法,但没有成功。 A级如何在一个类中创建复制构造函数和析构函数,其中对象本身有一个指向数组的指针数组
定义:
class A {
public:
A::A(const A& other){
siz = other.siz;
c = other.c;
s = other.s;
e = other.e;
arr= new int[other.c];
memcpy(arr, other.arr, sizeof(int) * c);
}
A::~A() {
delete [] m_arr;
}
const A& operator=(const A& rhs){
if(this == &rhs)
return *this; // handling of self assignment
delete[] arr; // freeing previously used memory
arr = new int[rhs.c];
siz = rhs.siz;
c = rhs.c;
e = rhs.e;
s = rhs.s;
memcpy(m_arr, rhs.arr, sizeof(int) * c);
return *this;
}
private :
int *arr ;
int c ;
int siz ;
int s ;
int e ;
}
B类的定义:
class B {
public:
B::B(const B& other){
// .......need help here
}
B::~B() {
//......need help here
}
private :
static const int constant = 7;
A * array[constant] ;
int x ;
int y ;
int z ;
}
感谢您的帮助
为什么不使用'std :: vector'? – user463035818
B :: B(const A&other){,为什么要在这里类A?它应该是B类 – Sumeet
为A写一个代理操作,我可以帮忙 – doctorlove