You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by "Max M." <ed...@maxim.comm2000.it> on 2000/06/06 00:20:35 UTC
cast from DOM_Node to DOM_Element
Hi, all.
What is the "right" way to cast from a DOM_Node to a DOM_Element?
I am tempted to think the following technique is perfectly safe...
void somefunc( DOM_Node& node )
{
if( node.getNodeType() == DOM_Node::ELEMENT_NODE )
{
DOM_Element& elem = static_cast<DOM_Element&>( node );
...
Is it?
But what if node is passed *by value*, instead? I know it will probably
work anyway, as I know how DOM_* classes are internally implemented.
Nevertheless, the resulting code won't be very clean, as downcasting from
something that is not a pointer or a reference is not usually considered
as a nice thing.
Wouldn't it be cleaner to provide a general mechanism to perform
this sort of conversions? Something like the following, for example
(assuming each class includes a typedef for its own ref counted
implementation, named, say, impl_type ):
template< class TO, class FROM >
TO xerces_downcast( const FROM& other )
{
return TO(static_cast<TO::impl_type*>(other.fImpl));
}
(which should be made friend of any DOM_* class)
This would allows you to write:
DOM_Element elem = xerces_downcast<DOM_Element>( node );
Max