使用继承的指针列表复制构造函数或重载运算符=
Copy Constructor or overloading Operator= with an inherited list of pointers
我有一个从指针列表继承的类,例如:
Class C : protected list<Type*>
现在,我想重载运算符=(并编写复制构造函数(。我应该迭代列表,为列表中的每个指针创建一个新的类型吗?
void C::operator=(const C& c)
{
if(!(*this)==c))
{
clear();
for(list<Type*>::iterator it = c.begin(); it != c.end(); it++)
{
Type* cp = new Type((*it)); //or doing cp=&(*it) after the new
push_back(cp);
}
}
}
或者我可以这样做吗?
void C::operator=(const C& c)
{
if(!(*this)==c))
{
clear();
for(list<Type*>::iterator it = c.begin(); it != c.end(); it++)
{
Type* cp = it; //or cp=&(*it)
push_back(cp);
}
}
}
我已经编辑了我的答案,因为这是一个家庭作业练习
在一个普通的应用程序中,你不应该从STL容器派生,它们的析构函数不是虚拟的。因此,当C
被破坏时,std::list<T>
将保留,从而导致内存泄漏。它们一开始就不应该被继承。。。
在正常的设计中,您会将列表作为一个对象:
#include <list>
template<typename T>
class C {
private:
std::list<T*> lst;
public:
C& operator=(const C& c) {
if (this != &c) {
lst = c.lst;
}
return *this;
}
};
我认为GOOD练习是实现一个MyList
类,从头开始制作所有东西。但众所周知,教授会让学生做一些奇怪的、不合逻辑的事情。因此,假设您确实想从std::list
派生,并且只重载operator=
,自己复制
#include <list>
template<typename T>
class C : protected std::list<T*>
{
public:
constexpr C& operator=(C const& c) noexcept {
if (this != &c) {
this->clear();
// copy
}
return *this;
}
};
现在,你如何复制。。。多种口味!有一个很好的C风格循环:
for (int i = 0; i < c.size(); ++i) this->push_back(c[i]);
有一个迭代器循环:
for (std::list<T*>::const_iterator it = c.cbegin(); it != c.cend(); ++it) this->push_back(*it);
有一个带有auto
和广义访问器的迭代器循环:
for (auto it = std::cbegin(c); it != std::cend(c); ++it) this->push_back(*it);
有基于范围的循环:
for (auto const& el : c) this->push_back(el);
有一些算法,比如std::for_each
std::for_each(std::cbegin(c), std::cend(c), [this](T* ptr) { this->push_back(ptr); });
和std::copy
std::copy(std::cbegin(c), std::cend(c), std::back_inserter(*this));
注意,std::back_inserter
是在迭代时执行push_back
的迭代器。
在未来(C++20(,我们将有范围,所以你可以写一些类似的东西
std::ranges::copy(c, *this);
虽然我不确定这是正确的。。。
选择你的毒药!
这取决于你想做什么!
第一个执行深度复制;第二个只是执行列表中指针的浅拷贝,因此没有做原始std::list
实现没有做的任何事情
此外,if(!(*this)==c))
是错误的;它有太多的CCD_ 13,并且可能是有意成为CCD_。
相关文章:
- 当有右值构造函数可用时,为什么从右值调用类引用构造函数重载?
- 两种类型的构造函数重载
- C++ 构造函数重载 - 为什么说我需要括号?
- C++具有多重继承的构造函数重载解析
- C++,处理多个构造函数重载和冗余代码
- c++ 构造函数重载歧义与initializer_list
- 父类有 26 个构造函数重载.如何在不复制+粘贴 26 个重载的情况下将一个小任务附加到所有构造器?
- 用户定义的构造函数重载与参数超类的重载不匹配
- 模板专用类之间的构造函数重载
- 指向构造函数重载的指针?
- C++简化了构造函数重载
- 类构造函数重载不起作用
- C++类模板构造函数 -- 重载引用 (U&) 与数组 (U*) 失败
- 为什么存在 C++11 std::initializer_list 构造函数重载规则
- 构造函数重载和默认参数
- 模板类构造函数重载解析
- 单例中的构造函数重载不起作用
- 构造函数重载选择了强制转换运算符而不是结构类型
- c++ 11构造函数重载解析和initialiser_lists: clang++和c++不一致.< / h1 &
- C++构造函数重载的经验法则