Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何有效地将信息从类tXmlDoc传递到类tXmlShape_C++ - Fatal编程技术网

C++ 如何有效地将信息从类tXmlDoc传递到类tXmlShape

C++ 如何有效地将信息从类tXmlDoc传递到类tXmlShape,c++,C++,以下任务: 我有一个xml文件,其中包含需要处理的数据。这些数据分组在以分层方式组织的各个节点中。为了简单起见,我稍微简化了xml文件的结构: <doc_root> <case_def> <shape number="1"> ... shape data ... </shape number> <shape number="2">

以下任务: 我有一个xml文件,其中包含需要处理的数据。这些数据分组在以分层方式组织的各个节点中。为了简单起见,我稍微简化了xml文件的结构:

<doc_root>
   <case_def>
      <shape number="1">
          ... shape data ...
      </shape number>
      <shape number="2">
          ... shape data ...
      </shape number>
   </case_def>
   <parameters>
   </parameters>
</doc_root>
现在为了访问和处理
shape
节点中的数据,我创建了一个类
tShapeNode

#include <iostream>
#include <string>
#include <charconv> 
#include "../../libs/pugixml-1.11/src/pugixml.hpp"
#include "XmlNodes.h"
#include "tXmlDoc.h"

namespace nXml
{
    class tXmlShape
    {
    public:
        tXmlShape();
        tXmlShape(const int shape_id);
        ~tXmlShape();

        pugi::xml_node GetShapeNode(const int);

        int GetShapeId();
        int GetAttributNumber();

        std::string GetShapeType(const int shape_id);
        std::string GetDescription(const int shape_id);

        void LoadData();

    protected:
        tXmlDoc xml_doc;

        pugi::xml_node shape_node;

        int shape_id = 0;
        int n_attr = 0;

        std::string description;
        std::string shape_type;
    }
    ;

}
;
#包括
#包括
#包括
#包括“../../libs/pugixml-1.11/src/pugixml.hpp”
#包括“XmlNodes.h”
#包括“tXmlDoc.h”
名称空间nXml
{
类tXmlShape
{
公众:
tXmlShape();
tXmlShape(const int shape_id);
~tXmlShape();
pugi::xml_节点GetShapeNode(const int);
int GetShapeId();
int getAttributeNumber();
std::字符串GetShapeType(const int shape_id);
std::string GetDescription(const int shape_id);
void LoadData();
受保护的:
tXmlDoc xml_doc;
pugi::xml_节点形状_节点;
int shape_id=0;
int n_attr=0;
std::字符串描述;
std::字符串形状\u类型;
}
;
}
;
如您所见,我已将
tXmlDoc
成员添加到我的
tXmlShapeNode
中,以便我可以从
tXmlDoc
访问
tXmlShapeNode
所需的数据。为了访问存储在
节点中的数据,我需要
文档根
节点的句柄。因此,我们的想法是使用
tXmlShapeNode
的构造函数将数据加载到
tShapeNode
的本地
txmldocxml\u doc
成员中。但是,我必须对xml文件中的每个形状执行此操作,这在内存和操作方面似乎效率不高

现在我有两个问题:

  • 如何将存储在
    tXmlDoc.root\u节点中的信息传递给
    tXmlShapeNode
    ,而不必为我拥有的每个形状处理xml文档
  • 有没有一种方法可以避免将文件名和文件路径信息传递给tXmlDoc类的全局变量
  • 回答第一部分

    形状类xml_doc不是一个tXmlDoc对象,而是一个引用实际tXmlDoc对象的对象

    使用C++引用< /P>

    //注意“&”
    tXmlDoc&xml_-doc;
    
    还是指针

    
    //注意“*”
    tXmlDoc*xml_-doc;
    
    这两个选项都可以用于访问某些远程对象,如document类。通常,您会将对doc类的引用传递给构造函数中的shape类

    在这里,构造函数使用一个引用:

    类tXmlShape
    {
    公众:
    //tXmlShape();//现在您的构造函数必须包含一个引用,所以这不起作用
    tXmlShape(const int id,tXmlDoc&doc):xml_doc(doc),shape_id(id){}
    //...
    tXmlDoc&xml_-doc;
    int-shape_-id;
    };
    
    这里使用指针:

    
    类tXmlShape
    {
    公众:
    //为清楚起见,初始化构造函数中的成员
    tXmlShape():xml_doc(nullptr),shape_id(0){
    tXmlShape(const int id,tXmlDoc*doc):xml_doc(doc),shape_id(id){}
    //...
    tXmlDoc*xml_-doc;
    int-shape_-id;
    };
    

    引用并非没有问题,当然主要是为了确保它们所引用的对象确实存在。这是C++的一个主要方面,它在这篇文章中太复杂了,但你会发现许多关于如何处理引用的文献[/P>。所以我能正确地理解,在我的代码> THealEnNODE I类定义了成员<代码> TXMLDOC和XMLXDOC < /C>?我现在如何将信息传递给此引用?这个引用如何知道我引用的是哪个实例?我添加了一些关于如何在tXmlShape构造函数中传递参数的信息。只需从引用的对象{int x;int&ref_to_x=x;}初始化引用即可。对于函数,函数参数的类型将选择是通过引用还是通过复制等方式传递对象
    #include <iostream>
    #include <string>
    #include <charconv> 
    #include "../../libs/pugixml-1.11/src/pugixml.hpp"
    #include "XmlNodes.h"
    #include "tXmlDoc.h"
    
    namespace nXml
    {
        class tXmlShape
        {
        public:
            tXmlShape();
            tXmlShape(const int shape_id);
            ~tXmlShape();
    
            pugi::xml_node GetShapeNode(const int);
    
            int GetShapeId();
            int GetAttributNumber();
    
            std::string GetShapeType(const int shape_id);
            std::string GetDescription(const int shape_id);
    
            void LoadData();
    
        protected:
            tXmlDoc xml_doc;
    
            pugi::xml_node shape_node;
    
            int shape_id = 0;
            int n_attr = 0;
    
            std::string description;
            std::string shape_type;
        }
        ;
    
    }
    ;