c++ - 从第三方模块复制成员时编写复制构造函数的任何提示被拒绝?
问题描述
我目前正在编写一些从 xml 文件中读取数据的代码。为此,我使用了 pugixml 模块,该模块提供了一个类xml_document
来提供解析文件的方法。为了存储这些数据,我创建了一个tXmlDoc
管理加载操作的类(如设置路径、存储解析结果等)。我现在想为此包含一个复制构造函数,tXmlDoc
但是,唉,我无法复制该pugi::xml_document
成员,因为 pugixml 显然明确地拒绝复制,就我了解此模块的代码而言。
你有什么建议如何处理这个问题?我不想为 pugixml 编写一个复制构造函数,因为我确信作者有充分的理由否认这一点,我不想摆弄那个。
顺便说一句,我对赋值运算符有类似的问题。
看起来在 pugixml 中只实现了一个移动构造函数。
这是我当前的tXmlDoc
标题。
#pragma once
#include <iostream>
#include <string>
#include <array>
#include "../../libs/pugixml-1.11/src/pugixml.hpp"
#include "XmlData_General.h"
#include "XmlNodes.h"
namespace nXml
{
/**
* This class provides methods and helper functions to:
* - read an xml file identified by its file name including suffix and the path to this file
* - load the root node of the xml document
* - to manage basic information of the root node
*/
class tXmlDoc
{
public:
tXmlDoc();
tXmlDoc(tXmlDoc&); //copy constructor
~tXmlDoc();
tXmlDoc& operator=(const tXmlDoc&);
void LoadXmlFile(const std::string& file_name, std::string& file_path);
pugi::xml_node GetRootNode();
std::string GetFileName();
std::string GetFilePath();
std::string GetFullPath(const std::string& file_name, std::string& file_path);
void SetFileName(const std::string& file_name);
void SetFilePath(const std::string& file_path);
bool GetFileLoaded();
bool GetNodeCorrect();
int GetErrorCode();
int GetChildNumber();
pugi::xml_parse_result GetXmlResult();
void LoadData();
void CheckNode();
protected:
std::string file_path = ""; //is set when calling LoadXmlFile
std::string file_name = ""; //is set when calling LoadXmlFile
bool file_loaded = false; //provides information is file was successfully loaded (file correctly loaded: true). Modified by method LoadXmlFile
bool node_correct = false; //indicating of node structure on its child level is correct
int error_code = 0; //provides information about occuring errors (no error: 0). Modified by method LoadXmlFile
int n_childs = 0;
pugi::xml_node root_node = pugi::xml_node(); //contains the root node of the xml document. Filled when calling LoadData (via calling GetRootNode inside LoadData)
pugi::xml_parse_result xml_result; //contains information about parsing result. Filled by method LoadXmlFile
pugi::xml_document xml_doc; //contains the entire document
//make sNodeNames known
sXmlNodeNames sNodeNames;
}
;
};
解决方案
是否可以手动复制:
我在这个主题上找到了这个问题
xml_document copy;
copy.reset(doc);
如果您不想真正复制它,另一种选择是将 shared_ptr 保留为 xml-root 的 const 版本。
推荐阅读
- python - 使用 OracleOperator 调用在 Airflow 中返回变量的存储过程
- django - 字段“门”需要一个数字,但得到了“”
- javascript - 从父级关闭子窗口并检索数据
- .net-core - JWT Expiration Time 访问令牌设置为一小时 .net 核心
- c++ - 为什么我不能在基于范围的循环之外使用 boost::adaptor::map_values?
- java - 需要从 Mon 中提取数据
- reactjs - 字体加载在本地服务器上但未在生产中
- contentful - 如何在 Contentful 中为下拉字段设置不同的标签和值
- java - java AES 加密方法的这条返回线是什么?
- javascript - 电子保存的 mp3 不播放