This is a result of how DOMString was implemented. Think of it more like
Java's StringBuffer class. For example, this code might surprise you:
void
foobar(const DOMString& theString)
{
DOMString foo(theString);
foo += "bar";
}
void
foobarClone(const DOMString& theString)
{
DOMString foo(theString.clone());
foo += "bar";
}
void
bar()
{
DOMString foo1("foo");
DOMString foo2("foo");
foobar(foo1);
foobarClone(foo2);
// foo1 is now equal to "foobar"
// foo2 is still equal to "foo"
}
The meta issue here is that I think this is a very confusing implementation
for C++ programmers -- most would think that a DOMString is more like the
typical C++ string class (std::string, for example), rather than a Java
StringBuffer. I predict this will be a frequent source of
very-hard-to-find bugs. Imagine geting a value from a node and
inadvertantly modifying that string:
DOMString theNodeName = node.getNodeName();
theNodeName += "foo";
I've now changed the name of the node.
Dave
"Arnold, Curt" <[EMAIL PROTECTED]> on 11/18/99 04:06:51 PM
Please respond to [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
cc: (bcc: David N Bertoni/CAM/Lotus)
Subject: cloning DOMString's on assignment in Xerces-C
I've noticed in some places string assignments first clone the right hand
side and in other places they don't. I don't know if this is intentional,
an artifact of the derivation from XML4J, or just an unnecessary bit of
overhead. If someone could enlighten me, I'd appreciate it.
For example:
This constructor clones nothing:
NodeImpl::NodeImpl(DocumentImpl *ownerDoc,
const DOMString &nam, short nTyp,
bool isLeafNod, const DOMString &initValue)
{
// Do we want to add isLeafNode to this? How about initial value?
this->ownerDocument=ownerDoc;
this->namespaceURI=null; //DOM Level 2
this->prefix=null; //DOM Level 2
this->localName=nam; //DOM Level 2
this->name=nam;
this->nType=nTyp;
this->isLeafNode=isLeafNod;
this->value=initValue;
while this constructor clones everything
NodeImpl::NodeImpl(const NodeImpl &other, bool deep) {
this->nType = other.nType;
this->namespaceURI = other.namespaceURI.clone(); //DOM Level 2
this->prefix = other.prefix.clone(); //DOM Level 2
this->localName = other.localName.clone(); //DOM
Level 2
this->name = other.name.clone();
this->value = other.value.clone();
this->isLeafNode = other.isLeafNode;
this->readOnly = false;