pImpl 클래스의 복사본 작성자에게 연락하고 싶다면 어떻게해야할까요? 예 :pimpl 클래스에서 호출자 클래스의 복사 생성자를 호출하는 방법은 무엇입니까?
CImpl::SomeFunc()
{
//cloning the caller class instance
caller = new Caller(*this)// I cant do this since its a pImpl class
}
어떻게하면됩니까?
pImpl 클래스의 복사본 작성자에게 연락하고 싶다면 어떻게해야할까요? 예 :pimpl 클래스에서 호출자 클래스의 복사 생성자를 호출하는 방법은 무엇입니까?
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);
이 질문에 대답하는 것은 매우 어렵다 제대로 물어 보지 않았다. 닫히기 위해 투표했습니다. – Nawaz
내 말은 내가 호출자 클래스의 copyconstructor를 해당 pImpl 클래스에서 호출해야하는 상황이다. 어떻게 할 수 있습니까? – Yogi
그렇게해야한다면, 디자인이 잘못되었습니다. – Nawaz