- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在下面的代码中,我有一个 A 类,它有一个动态的整数数组。我有另一个 B 类,它有一个指向 A 类对象的指针数组。我已经为 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 ;
}
感谢帮助
最佳答案
关键是将原始 owning 指针包装到 RAII 资源管理器 中,并定义组装这些安全 RAII 构建 block 的类。然后,C++ 编译器将能够自动合成复制操作和析构函数(以及移动操作)。
例如,在您的 class A
你有一个int *arr
数据成员,用于存储指向动态分配数组的拥有 原始指针。将其替换为 RAII 资源管理器,如标准 std::vector
容器(例如 std::vector<int> arr
)。
这样一来,就无需定义显式析构函数,因为编译器将自动调用 std::vector
vector 数据成员上的析构函数,内存将自动释放(无需显式调用 delete[]
)。
类似地,默认复制构造函数将执行成员复制,因此 std::vector
复制构造函数将被 C++ 编译器自动调用,并且从源 vector 到目标 vector 的深度复制将自动发生,无需您使用 new[]
来“重新发明轮子”动态分配,memcpy
等
从 C++11 开始,您可以告诉 C++ 编译器使用此语法合成默认复制构造函数(也可用于默认构造函数、移动构造函数、复制赋值运算符等)
class A {
public:
...
A(const A&) = default;
...
};
class B
也是如此, 而不是 A * array[constant]
数据成员,考虑使用 vector
,例如指向 A 的智能指针 vector :
vector<shared_ptr<A>> arrayOfAs;
替代方法可能是使用 std::array
对于大小不变的东西。
无论如何,关键是:考虑使用 RAII 资源管理器作为更复杂类的构建 block ,而不是将原始拥有指针和其他不安全的原始资源作为数据成员。每个原始资源都应该安全地包装在自己的 RAII 资源管理器中,而这又应该用作更复杂类的数据成员。
P.S. 作为额外阅读,请考虑阅读 "What is The Rule of Three?" , 而这种 follow up .
关于c++ - 如何在具有指向对象的指针数组的类中创建复制构造函数和析构函数,其中对象本身具有指向 int 的指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46423735/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!