0

I just need to know if I want to call my copyconstuctor from pImpl class, how will I do it? For example:

CImpl::SomeFunc()
{

//cloning the caller class instance

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

}

How can i achieve this?

Yogi
  • 1,035
  • 2
  • 13
  • 39
  • It is very difficult to answer a question which is not asked properly. Voted for close. – Nawaz Feb 20 '12 at 03:09
  • what i mean is i have a situation where i need to call copyconstructor of my caller class in its corresponding pImpl class. How will I do it? – Yogi Feb 20 '12 at 03:11
  • If you need to do that, your design is wrong to begin with. – Nawaz Feb 20 '12 at 03:13
  • Search for it...on this site...and google. – Nawaz Feb 20 '12 at 03:18
  • I am not able to find it.can you please provide some links? – Yogi Feb 20 '12 at 03:22
  • Yogi: what is your implementation as of now? – Nawaz Feb 20 '12 at 03:24
  • Its like all my private data members are there in pImpl class. now i have to clone tht instance of a caller and return. I have a pImpl instace in my caller and i am doing copy and assignment for the pImpl in my caller class.so somehow i need to call tht copy constructor. – Yogi Feb 20 '12 at 03:29
  • Also, if i call copy constructor of my pImpl class in pImpl only,then I face the problem of converting tht pImpl instance to my caller. I hope you get my problem. – Yogi Feb 20 '12 at 03:31
  • Why are you not implementing the copy constructor for the Caller class, where you could also make a hard copy of `m_pImpl` pointer? That is how it should be done. something like this : `Caller(Caller const & other) : m_pImpl(other.m_pImpl->Clone()) {}` – Nawaz Feb 20 '12 at 03:33
  • I have implemented copy constructor in the caller class Caller(caller& val) { *pImpl = *val.pImpl } I just want this to be called from my pImpl class? – Yogi Feb 20 '12 at 03:39
  • Yogi: Why you want it be called from pimpl class? Anyway, see my answer. – Nawaz Feb 20 '12 at 03:46

1 Answers1

3

Well after reading your comments, it seems that you want to be able to provide the ability to make copies of Caller class. If so, then in that case you should implement the copy constructor for Caller class, where you can make a hard copy of m_pImpl pointer.

class CallerImpl;

class Caller
{
   std::shared_ptr<CallerImpl> m_pImpl;
public:
   Caller(Caller const & other) : m_pImpl(other.m_pImpl->Clone()) {}
   //...
};

And then you can implement Clone() function in CallerImpl class as:

class CallerImpl
{
   public:
     CallerImpl* Clone() const
     {
         return new CallerImpl(*this); //create a copy and return it
     }
     //...
};

Now you can make copy of Caller:

//Usage
Caller original;
Caller copy(original); 
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • How do i call this copy constructor from my Pimpl class? – Yogi Feb 20 '12 at 03:55
  • 2
    @Yogi: WHY WHY WHY? Why do you want it be called from `Pimpl` class? Why it is needed? – Nawaz Feb 20 '12 at 03:56
  • That is what is needed..I have everything in place.is it possible some how?if not then may be I need to change my design – Yogi Feb 20 '12 at 03:59
  • @Yogi: As I said in the beginning of our conversation, that if you ever need to do that, then your design is wrong to begin with. And I don't even know how you're doing that, and why you need that. Your post gives no information regarding that. – Nawaz Feb 20 '12 at 04:00
  • hmm, Because all my implementation is moved to pImpl class. I am just caaling pImpl metods from corresponding caller methods. so one API is there which should return all instances of the caller class.so i need to clone instances with all data members and return. – Yogi Feb 20 '12 at 04:04
  • @Yogi: Making a copy of the `Caller` object is equivalent to making a copy of `CallerImpl` object, isn't it? So when you make a copy of `Caller` class, which in turn makes a copy of the underlying `m_pImpl` member, as shown in my answer. Did you see and understand my code? – Nawaz Feb 20 '12 at 04:07
  • @Yogi: Also, why do you have a method in `Caller` which makes a copy? Why can't you use copy-constructor for that? – Nawaz Feb 20 '12 at 04:09