将原始指针转换为clone_ptr
我有一个 clone_ptr 实现,如 这个问题,我有一个问题,我需要从函数返回的原始指针创建一个clone_ptr。
这是代码:
DOMDocument* doc = impl->createDocument(
0, // root element namespace URI.
XML::X(docname.c_str()), // root element name
0); // document type object (DTD).
document.get() = *doc; //No way to assign clone_ptr document to raw doc pointer
其中document
& impl
声明如下:
clone_ptr<DOMImplementation, default_clone<DOMImplementation> > impl;
clone_ptr<DOMDocument, default_clone<DOMDocument> > document;
上面的 createDocument
函数返回一个原始 DOMDocument
指针,并分配给局部变量 doc
>,现在我想获取我的文档clone_ptr,并实际向它传递从创建文档函数获取的原始指针。然而编译器似乎对此不太满意,因为它说了以下内容:
error C2440: '=' : cannot convert from 'xercesc_3_1::DOMDocument' to 'clone_ptr<T,Cloner>::pointer'
with
[
T=xercesc_3_1::DOMDocument,
Cloner=default_clone<xercesc_3_1::DOMDocument>
]
所以我的问题是如何允许原始指针显式或隐式转换为clone_ptr? 编辑:
克隆专业化:
template<typename T>
struct default_clone
{
static T* clone(T* pPtr)
{
return pPtr ? pPtr->clone() : 0;
}
};
template<>
struct default_clone<DOMDocument>
{
static DOMDocument* clone(DOMDocument* pPtr)
{
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(XML::X("Core"));
return pPtr ? impl->createDocument(0, XML::X(""), 0) : 0;
}
};
template<>
struct default_clone<DOMImplementation>
{
static DOMImplementation* clone(DOMImplementation* pPtr)
{
return pPtr ? DOMImplementationRegistry::getDOMImplementation(XML::X("Core")) : 0;
}
};
I have a clone_ptr implementation, as was shown in this question and I have a problem where I need to create a clone_ptr from a raw pointer returned from a function.
Here is the code:
DOMDocument* doc = impl->createDocument(
0, // root element namespace URI.
XML::X(docname.c_str()), // root element name
0); // document type object (DTD).
document.get() = *doc; //No way to assign clone_ptr document to raw doc pointer
Where document
& impl
are declared as follows:
clone_ptr<DOMImplementation, default_clone<DOMImplementation> > impl;
clone_ptr<DOMDocument, default_clone<DOMDocument> > document;
The createDocument
function above returns a raw DOMDocument
pointer and is assigned to the local variable doc
, now I want to get my document clone_ptr and actually pass it the raw pointer gotten from the create document function. It seems however the compiler is not too happy with this as it says the following:
error C2440: '=' : cannot convert from 'xercesc_3_1::DOMDocument' to 'clone_ptr<T,Cloner>::pointer'
with
[
T=xercesc_3_1::DOMDocument,
Cloner=default_clone<xercesc_3_1::DOMDocument>
]
So my question is how can I allow a raw pointer to be explicitly or implicitly converted to a clone_ptr
?
EDIT:
Clone specialization:
template<typename T>
struct default_clone
{
static T* clone(T* pPtr)
{
return pPtr ? pPtr->clone() : 0;
}
};
template<>
struct default_clone<DOMDocument>
{
static DOMDocument* clone(DOMDocument* pPtr)
{
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(XML::X("Core"));
return pPtr ? impl->createDocument(0, XML::X(""), 0) : 0;
}
};
template<>
struct default_clone<DOMImplementation>
{
static DOMImplementation* clone(DOMImplementation* pPtr)
{
return pPtr ? DOMImplementationRegistry::getDOMImplementation(XML::X("Core")) : 0;
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
提供您的
clone_ptr
实现,而doc
是一个指针,那岂不是document.reset(doc)
?Giving your
clone_ptr
implementation, and the fact thatdoc
is a pointer, wouldn't it bedocument.reset(doc)
?我不知道这个库,但如果 document.get() 返回一个左值(因此你给它分配一些东西看起来相当奇怪),我会感到非常惊讶。这并不意味着它不会编译,因为很少有人将返回类型实现为 const(即返回一个常量作为临时变量),只是分配不会达到预期的效果。
I don't know the library but would be very surprised if document.get() returned an l-value (thus your assigning something to it seems rather strange). That doesn't mean it won't compile as very few people implement return types as const (ie. returning a constant as the temporary), just that the assign won't have the desired effect.