template<class T,int SIZE=20> class Stack { public: Stack() { this->size = SIZE; this->p = new T[SIZE]; memset(this->p, 0, sizeof(T) * SIZE); } Stack(int n) { if (n <= 0) n = SIZE; this->size = n; this->p = new T[n]; memset(this->p, 0, sizeof(T) * n); } Stack(const Stack<T>& stac) { this->size = stac.size; this->p = new T[stac.size]; for (int i = 0; i < stac.size; ++i) { this->p[i] = stac.p[i]; } }这样写的拷贝构造函数正确吗
时间: 2024-04-04 22:30:25 浏览: 15
这样写的拷贝构造函数是不正确的,因为它没有考虑到浅拷贝可能导致的问题。在进行浅拷贝时,拷贝构造函数只是复制了指针的值,而没有复制指针所指向的堆内存中的数据。这意味着,如果原对象和拷贝对象指向同一块堆内存时,当一个对象销毁时,它会释放这段内存,这将导致另一个对象指向的内存变成非法的。因此,在实现拷贝构造函数时,需要进行深拷贝,即重新分配一块内存并将原对象的数据复制到新的内存中。
相关问题
template<class T,int OTER_SIZE> friend class Stack<T, OTER_SIZE>;友元模板声明无效
如果在类中声明了友元模板类,那么它的模板参数列表需要与被声明类的模板参数列表完全一致,并且需要在类外部再次声明一遍。例如,如果我们在`Stack`类中声明了一个友元模板类`OtherStack`,那么我们需要这样写:
```
template<class T, int OTHER_SIZE>
class OtherStack;
template<class T, int SIZE>
class Stack {
public:
// ...
private:
// ...
template<class T, int OTHER_SIZE>
friend class OtherStack;
};
```
注意,这里的`OtherStack`类模板声明需要在类外部进行,而且模板参数列表需要与类内部声明的完全一致。
template<int OTER_SIZE> Stack(const Stack<T,OTER_SIZE>& stac) { this->size = stac.size; this->p = new T[stac.size]; for (int i = 0; i < stac.size; ++i) { this->p[i] = stac.p[i]; } }这段代码显示无法访问private成员
这段代码无法访问`Stack`类的私有成员是因为在拷贝构造函数中,我们尝试访问`size`和`p`成员变量,但是这些成员变量是`Stack`类的私有成员,不能直接从类外部访问。
要解决这个问题,我们可以使用`friend`关键字来声明拷贝构造函数是`Stack`类的友元函数,从而允许它访问`Stack`类的私有成员。修改后的代码如下:
```
template<class T, int SIZE>
class Stack {
public:
// 构造函数
Stack() {
this->size = SIZE;
this->p = new T[SIZE];
memset(this->p, 0, sizeof(T) * SIZE);
}
// 带参构造函数
Stack(int n) {
if (n <= 0)
n = SIZE;
this->size = n;
this->p = new T[n];
memset(this->p, 0, sizeof(T) * n);
}
// 拷贝构造函数
template<int OTHER_SIZE>
friend class Stack<T, OTHER_SIZE>;
template<int OTHER_SIZE>
Stack(const Stack<T, OTHER_SIZE>& stac) {
this->size = stac.size;
this->p = new T[stac.size];
for (int i = 0; i < stac.size; ++i) {
this->p[i] = stac.p[i];
}
}
// 析构函数
~Stack() {
delete[] this->p;
}
private:
T* p;
int size;
};
```
在拷贝构造函数的模板参数列表中,我们添加了一个`friend`声明,将`Stack`类的所有实例都声明为`Stack<T, OTHER_SIZE>`的友元类,从而允许拷贝构造函数访问私有成员`size`和`p`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)