实现复制构造函数 [英] implementing a copy constructor
问题描述
我有以下类定义,它需要一个复制构造函数,因此可以进行深层复制来复制原始指针。有人可以建议如何做到最好吗?
I have the following class definition and it needs a copy constructor so deep copies are made to copy the raw pointers. Can anybody advice on how to best do this?
使用 xerces-c ++ for XML
Using xerces-c++ for XML
class XMLDocument
{
private:
typedef std::vector<XML::XMLNode> v_nodes;
public:
XMLDocument::XMLDocument();
XMLDocument::XMLDocument(const XMLDocument& copy);
XMLDocument::~XMLDocument();
XMLDocument& operator=(XMLDocument const& rhs);
void CreateDocument(const std::string& docname);
void AddChildNode(XMLNode& node);
void RemoveNode(XMLNode& node);
void AddNodeValue(XMLNode& node, const std::string& value);
void AddNodeValue(XMLNode& node, int value);
void AddNodeValue(XMLNode& node, double value);
void AddNodeValue(XMLNode& node, float value);
std::string GetXMLAttributes();
std::string GetXMLAttribute(const std::string& attrib);
std::string GetXMLNodeText(XML::XMLNode& node);
std::string DumpToString();
XMLNode GetXPathNode(const std::string xpathXpression);
XMLNode GetNode(const XMLNode ¤tnode);
typedef v_nodes::iterator nodes_iterator;
nodes_iterator begin()
{
nodes_iterator iter;
iter = xmlnodes.begin();
return iter;
}
nodes_iterator end()
{
nodes_iterator iter;
iter = xmlnodes.end();
return iter;
}
private:
v_nodes xmlnodes;
bool InitializeXML();
DOMImplementation* impl; //Abstract
DOMDocument* document; //Abstract
DOMElement* rootelement; //Abstract
};
创建DOMDocument 调用函数,而因此 DOMElement。因此,我不能只是在这些指针上调用new。
The DOMDocument is created calling a function and so is the DOMElement. So I can't just call new on these pointers.
不确定我是否只是从字面上重新创建了所有这些对象?
Not sure if I just literally recreate all these object?
示例:
document = impl->createDocument(0, "mydoc", 0);
谁经历了一次投票失败却没有给出原因????
Who's gone on a downvote rage and not given a reason???
推荐答案
此页面可能会有所帮助:
http://icu-project.org/docs/papers/cpp_report/the_anatomy_of_the_assignment_operator.html
This page might certainly help:
http://icu-project.org/docs/papers/cpp_report/the_anatomy_of_the_assignment_operator.html
请注意,它们是编写复制构造函数和赋值运算符之间的区别,但是这一点也在本文中进行了讨论。
Note their is a difference between writing a copy-constructor and an assignment operator but this point is also discussed in the paper.
这篇关于实现复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!