-
December 10th, 2012, 09:41 AM
#1
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).
This is what I've tried, but it failes to compile.
Code:
PugiXmlNode PugiXmlNode::appendChild(std::string name)
{
return (PugiXmlNode) (append_child(name.c_str()));
}
Is there any way to do this?
W.
-
December 10th, 2012, 03:19 PM
#2
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.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 10th, 2012 at 03:28 PM.
-
December 11th, 2012, 03:49 AM
#3
Re: Derive from pugi::xml_node
Thanks for this excellent, easy to understand answer!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|