C++中的构造函数副本、列表、指针和模板
Constructor copy, list, pointers and template in C++
我想做一个复制构造函数,并为下面的类定义=运算符
template <class S, class T>
class Graphe
{
protected:
int prochaineClef;
public:
PElement< Sommet<T> > * lSommets; // liste de sommets
PElement< Arete<S,T> > * lAretes; // liste d'arêtes
Graphe(const Graphe<S,T> & graphe);
const Graphe<S,T> & operator = (const Graphe<S,T> & graphe);
}
到目前为止,我已经尝试过关于构造函数:
template <class S, class T>
Graphe<S,T>::Graphe(const Graphe<S,T> & graphe)
{
PElement< Sommet<T> > * nouvelListeSommet = new PElement<Sommet<T>>(*graphe.lSommets);
PElement< Arete<S,T> > * nouvelListeAretes = new PElement<Arete<S,T>>(*graphe.lAretes);
this->prochaineClef = graphe.prochaineClef;
this->lAretes = nouvelListelAretes;
this->lSommets = nouvelListeSommet;
//nouvelListeSommet = graphe.lSommets->copieListe(graphe.lSommets);
//nouvelListelAretes = graphe.lAretes->copieListe(graphe.lAretes);
}
So i got this error saying
visual studio 2012projectsihmtp2graphetp2graphepelement.h(123): error C2664: 'PElement<T>::PElement(T *,PElement<T> *)' : can't convert param1 from 'PElement<T> *const ' to 'Sommet<T> *'
1> with
1> [
1> T=Sommet<InfoSommetCarte>
1> ]
1> and
1> [
1> T=Sommet<InfoSommetCarte>
1> ]
1> and
1> [
1> T=InfoSommetCarte
1> ]
这是我的PElement类:
class PElement
{
public :
T * v;
PElement<T> * s;
PElement( T * v, PElement<T> * s );
PElement(PElement<T> & l);
}
template<class T>
PElement<T>::PElement(PElement<T> & l)
{
//this->v = new T(l->v);
this = new PElement<T>(l,this);
}
我不知道如何修复我的复制构造函数PElement这个->v=新T(l->v)正确吗?
这是我的错误副本列表方法:
/*
template<class T>
PElement<T> * PElement<T>::copieListe(PElement<T> * original)
{
for(int i = 0; i < PElement<T>::taille(original);i++)
{
this->insertionTete(original->v,this);
original = original->s;
}
return this;
}
*/
如果列表不为空,则需要为PElement类复制一个复制初始值设定项,用于复制下一个元素。
template <class T>
class PElement
{
public :
T * v;
PElement<T> * s;
PElement(T * v, PElement<T> * s = nullptr);
PElement(const PElement<T> & l);
};
template<class T>
PElement<T>::PElement(T * v, PElement<T> * s) : v(v), s(s) {}
template<class T>
PElement<T>::PElement(const PElement<T> & l)
{
v = new T(*l.v);
s = l.s ? new PElement<T>(*l.s) : nullptr;
}
这就是图类的副本初始化器
template <class S, class T>
Graphe<S,T>::Graphe(const Graphe<S,T> & graphe)
{
lSommets = graphe.lSommets ? new PElement<Sommet<T>>(*graphe.lSommets) : nullptr;
lAretes = graphe.lAretes ? new PElement<Arete<S,T>>(*graphe.lAretes) : nullptr;
prochaineClef = graphe.prochaineClef;
}
我不会用这种方式实现它,因为列表是递归复制的,如果列表很长,可能会出现堆栈溢出。按照@Rerito的建议,您应该使用std::list。这个代码示例向您展示了应该如何进行操作以避免出现错误。
PElement的复制构造函数需要对PElement进行引用。这就是我们传递*graphe.lSommets和*graphe.l Aretes.
表达式graphe.lSommets
是指针,并且没有PElement
构造函数接受指针。
这可以通过创建一个接受指针的新构造函数来解决,或者(我建议的方式)取消引用指针:
new PElement<Sommet<T>>(*graphe.lSommets);
// ^
// |
// Note the dereference operator
以下PElement<T>
的复制构造函数应该可以工作。
template <typename T>
PElement<T>::PElement(const PElement<T> &o) : v(nullptr), s(nullptr) {
// I assume you are marking the end of the list by a nullptr sentinel
if (nullptr != o.s) {
s = new PElement<T>(*o.s);
}
// Two options for `v`, pick one...
v = o.v; // Shallow copy of the T pointers
v = new T(*o.v) // Deep copy of the T pointers... Assume T is copyable
}
它将递归地复制列表中的每个元素,并在点击nullptr
sentinel时停止。
如果您想保持PElement
的原样(使用T*
来保存模板类型的东西),您可能需要执行深度复制(因此您选择this->v = new T(*o.v)
),否则您可能会考虑按值存储在列表元素中。
然后,您可以在图形复制构造函数中使用它:
template <typename S, typename T>
Graphe<S,T>::Graphe(const Graphe<S,T> &g) {
lSommets = new PElement<Sommet<T>>(*g.lSommets);
lAretes = new PElements<Arete<S,T>>(*g.lAretes);
// ... Whatever work you need ...
prochaineClef = g.prochaineClef;
}
相关文章:
- 为什么这个 std::queue/指向结构的指针列表直到 List.Size() == 0 才释放内存?
- 什么更好?返回对象指针列表?或返回指向对象列表的指针?
- 如何将共享指针列表传递到pybind11中的c++对象
- C++:如何初始化包含具有给定键的指针列表的映射
- 使用继承的指针列表复制构造函数或重载运算符=
- 我应该如何在工厂类中存储弱指针列表?
- Qt单元测试:指针列表上的QCOMPARE函数
- 如何正确释放要配对的指针列表?
- 如何在 c++ 上对指针列表进行排序
- 从地图复制到指针列表
- 我对C 中共享指针列表进行排序的功能未完成类型
- const_iterator引用的指针列表
- 在 c++ 中生成字符串到 std::指针列表的映射
- 访问链表中的结构(带有指向其子实现的指针列表的树)
- 有没有办法使用基于范围的迭代器来迭代对指针列表值的引用?
- 使用迭代器指针 (C++) 从对象指针列表中删除元素
- 无法列出定制类型的指针列表
- 创建指针列表的正确范例是什么
- 如何将指向指针列表的私有指针作为 const 返回
- C++填充指向指针**列表的指针,该列表指向指针地址