首页 > 解决方案 > 从第三方模块复制成员时编写复制构造函数的任何提示被拒绝?

问题描述

我目前正在编写一些从 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;
    }
    ;

};

标签: c++copy-constructor

解决方案


是否可以手动复制:

我在这个主题上找到了这个问题

xml_document copy;
copy.reset(doc);

如果您不想真正复制它,另一种选择是将 shared_ptr 保留为 xml-root 的 const 版本。


推荐阅读