If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Derive from pugi::xml_node

Hi,

I would like to derive a class from pugi::xml_node to add more functionality to the class.
I get stuck when I try to append a child because pug::xml_node append_child return pugi::xml_node (not a pointer).

Re: Derive from pugi::xml_node

Originally Posted by woomla

Hi,

I would like to derive a class from pugi::xml_node to add more functionality to the class.

First, does this pugi class have a virtual destructor? If not, then the intentions of the author of pugi is that it is not to be derived from. The class is meant to be used as a component within a larger framework, and not to be derived from or "tinkered with" by extending it.

If I give you a 6 foot ladder, and then you glue on an extra 2 rungs to this ladder, don't blame me if the ladder collapses when you step on those extra two rungs. That's what you're doing when you attempt to extend a class that isn't meant to be extended. In this case, you should be creating a class that uses a pugi object, or create wrapper functions that manipulates the object.

If the pugi class does have a virtual destructor, then you should investigate the valid, proper ways to add functionality (usually there would be virtual functions in the public or protected area of the class).

The reason for the virtual destructor is to make sure that if the class is used polymorphically, where the base class pointer may be used to destroy the object, then the destructors are called without undefined behaviour occurring. In other words, a virtual destructor guarantees that any usage at all of a derived object will work correctly with respect to destruction. Without a virtual destructor, the class is opened up to all sorts of problems if the coder happens to use the derived class in a polymorphic fashion.

Edit:

I took a look at this pugi class. The xml_node class does not have a virtual destructor, therefore derivation is not intended for this object. Other objects do have virtual destructors, so the authors are or should be fully aware of why they did not place a virtual destructor in the xml_node class.