我只需要知道我是否想从 pImpl 类中调用我的 copyconstructor,我该怎么做?例如:
CImpl::SomeFunc()
{
//cloning the caller class instance
caller = new Caller(*this)// I cant do this since its a pImpl class
}
我怎样才能做到这一点?
我只需要知道我是否想从 pImpl 类中调用我的 copyconstructor,我该怎么做?例如:
CImpl::SomeFunc()
{
//cloning the caller class instance
caller = new Caller(*this)// I cant do this since its a pImpl class
}
我怎样才能做到这一点?
好吧,在阅读了您的评论之后,您似乎希望能够提供复制Caller
课程的能力。如果是这样,那么在这种情况下,您应该为Caller
类实现复制构造函数,您可以在其中制作m_pImpl
指针的硬拷贝。
class CallerImpl;
class Caller
{
std::shared_ptr<CallerImpl> m_pImpl;
public:
Caller(Caller const & other) : m_pImpl(other.m_pImpl->Clone()) {}
//...
};
然后你可以在类中实现Clone()
函数:CallerImpl
class CallerImpl
{
public:
CallerImpl* Clone() const
{
return new CallerImpl(*this); //create a copy and return it
}
//...
};
现在您可以复制Caller
:
//Usage
Caller original;
Caller copy(original);