1

Short version: Can I safely create a copy constructor for a class that has member pointer variables, one of which is on the heap, that are of type xercesc::XercesDOMParser* and xercesc::DOMDocument*? If so, how?

Long version:

I wrote a class to wrap a Xercesc document object. It has two member variables that are pointers to a XercesDOMParser and a DOMDocument. Here is the part of the class declaration that shows those two members:

class XMLigator {
private:
    xercesc::XercesDOMParser* _pParser;
    xercesc::DOMDocument* _pDocument;
    etc...

The pointers _pParser and _pDocument are initialized in the constructor. Here is an abbreviated version of the constructor with the relevant code. Notice that _pParser is created with the new operator and is on the heap:

XMLigator::XMLigator(string strFile) : _strFile(strFile), _pDocument(NULL) {
    xercesc::XMLPlatformUtils::Initialize();
    _pParser = new xercesc::XercesDOMParser();  
    _pParser->parse(_strFile.c_str());
    _pDocument = _pParser->getDocument();
}

The destructor deletes the member variable _pParser:

XMLigator::~XMLigator(void) {
    if (m_pParser) {
        delete _pParser;
    }
}

Until now I have prohibited the copy constructor and assignment operator by marking them private and not defining them.

Now I would like to, if possible, create a copy constructor for this class so I can make it a return type for a function, in this fashion:

XMLigator getOne() {
    XMLigator xml("/some/file.xml");
    return xml;
}

The first problem I see is my the destructor does a delete _pParser;. So I think that's a problem. I am pretty sure I have to remove the delete from the destructor. But then I have a memory leak.

I really have no idea how to approach this. Maybe it's not possible. Does anyone have an approach that I can use for this?

John Fitzpatrick
  • 4,207
  • 7
  • 48
  • 71
  • Is the downvoter trying to tell me that my question is poorly asked or that I shouldn't try to make a copy constructor for this class? Or something else? – John Fitzpatrick Aug 10 '13 at 02:21
  • (I am not the downvoter.) Does `XercesDOMParser` have a (public) copy ctor? And are you sure you want `getOne()` to return an object, and not a pointer? – Beta Aug 10 '13 at 03:19
  • Since your class owns a resource (memory) you probably do need a _copy constructor_ and _copy assignment operator_. See: [What is The Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – Blastfurnace Aug 10 '13 at 03:21
  • @Beta, I'll answer the second question first. I am not married to the idea of returning an object rather than pointer or reference. I was just thinking it would be nice if I returned an object (with an approrpriate copy constructor) that would do it's own cleanup when it goes out of scope in the caller (like occurs with `std::string` or `std::vector`). If I return a pointer I believe the caller might need to do cleanup explicitly with a `delete` or some form of `release()` method of my own creation. For the first question I have to do some research... – John Fitzpatrick Aug 10 '13 at 09:04
  • You don't have to do much research, just try it. – Beta Aug 10 '13 at 11:50

0 Answers0