【发布时间】:2017-08-17 04:41:35
【问题描述】:
我正在学习动态内存分配。我有以下类,其中“A 类”应该在构造函数中拥有一个动态分配的数组。还应修改复制构造函数和析构函数。这就是我目前所拥有的......
#include <iostream>
#ifndef A_HH
#define A_HH
#include "B.hh"
class A {
public:
A() { B *array = new B[12];}
A(const A&) { /* Do not know what to put here..*/ }
~A() { delete[] array;}
private:
//B array[12] ; <- This is the array that I have to modify so it becomes dynamic.
B *array;
} ;
#endif
【问题讨论】:
-
顺便说一句,您应该将
#include <iostream>放在标题保护中。 -
你应该习惯rule of three(或者c++11以后的rule of five)。此外,您可以查看copy and swap idiom。
标签: c++ constructor destructor copy-constructor dynamic-memory-allocation