0

我只需要知道我是否想从 pImpl 类中调用我的 copyconstructor,我该怎么做?例如:

CImpl::SomeFunc()
{

//cloning the caller class instance

caller = new Caller(*this)// I cant do this since its a pImpl class

}

我怎样才能做到这一点?

4

1 回答 1

3

好吧,在阅读了您的评论之后,您似乎希望能够提供复制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); 
于 2012-02-20T03:44:22.513 回答