0

I'm using RapidXml in a c++ program. Well ok no problem it works. I just do not understand why I must use pointers instead of variable values... If you take a look to the RapidXml wiki page, some examples are provided, this is the one provided by RapidXml developers:

#include <iostream>
#include <string>
#include "rapidxml-1.13/rapidxml.hpp"
#include "rapidxml-1.13/rapidxml_print.hpp"
int main(int argc, char** argv);
int main(int argc, char** argv) {
    using namespace rapidxml;
    xml_document<> doc;
    // xml declaration
    xml_node<>* decl = doc.allocate_node(node_declaration);
    decl->append_attribute(doc.allocate_attribute("version", "1.0"));
    decl->append_attribute(doc.allocate_attribute("encoding", "utf-8"));
    doc.append_node(decl);
    // root node
    xml_node<>* root = doc.allocate_node(node_element, "rootnode");
    root->append_attribute(doc.allocate_attribute("version", "1.0"));
    root->append_attribute(doc.allocate_attribute("type", "example"));
    doc.append_node(root);
    // child node
    xml_node<>* child = doc.allocate_node(node_element, "childnode");
    root->append_node(child);
    xml_node<>* child2 = doc.allocate_node(node_element, "childnode");
    root->append_node(child2);
    std::string xml_as_string;
    // watch for name collisions here, print() is a very common function name!
    print(std::back_inserter(xml_as_string), doc);
    std::cout << xml_as_string << std::endl;
    // xml_as_string now contains the XML in string form, indented
    // (in all its angle bracket glory)
    std::string xml_no_indent;
    // print_no_indenting is the only flag that print() knows about
    print(std::back_inserter(xml_no_indent), doc, print_no_indenting);
    // xml_no_indent now contains non-indented XML
    std::cout << xml_no_indent << std::endl;
}

Well, why does it use a pointer to xml_node???

I ask this because I need to a function to return a xml_node...

So if I do this:

xml_node<>* mynode = ... return *mynode;

is it ok?? Because I want to use the returned node and all its children later. Is it good to do in this way? If not, how should I do?

Andry
  • 16,172
  • 27
  • 138
  • 246

3 Answers3

3

Returning a pointer is probably done to avoid calling the copy constructor of the node. It is faster to just return a pointer, especially considering the node is probably already allocated somewhere internally.

They could have also returned a reference, but they might wish to retain the ability to return NULL on invalid calls.

If you need a xml_node, you could always dereference the pointer (check for NULL first). If you really want to use the returned node and its children later, though, it's probably best to just use the returned pointer with -> and pass the pointer around by value.

James
  • 5,355
  • 2
  • 18
  • 30
2

Well, why does it use a pointer to xml_node

probably because it's faster to return a pointer to the node than making a copy on return.

hhafez
  • 38,949
  • 39
  • 113
  • 143
-2

OK... well RapidXML and many others like Xerces do not return values but pointers so that the programmer cannot take the value around and make copies all the way... this is done in order to preserve memory...

Especially when talkig about DOM, but also for SAX it is almost the same, these parsers need to create a very complex memory allocation structure in the computer's RAM where the program is run. In order to provide performance and so on ALL CONSTRUCTORS and COPY-CONSTRUCTORS are PRIVATE.

Take a look at the library... you'll discover this nice trick ahah.

Well the reasons are the ones I reported and that everyone suggested me.

I guess that when using c, c++ and low level languages, it is not that immediate the way to program... a programmer cannot take a node and pass it or return to functions and classes very easily.

Andry
  • 16,172
  • 27
  • 138
  • 246
  • 3
    -1 for answering your own question well after two other people already answered it, and you basically repeated what they said just very long winded and not quite as coherently. Also you say "take a look at the library". Telling someone to go figure it out for themselves after asking others to help you and then stealing their answers. Not cool. –  Nov 27 '11 at 08:06