Search code examples
c++pimpl-idiom

Converting pImpl pointer back to caller type


I am using pimpl idiom in my program and I am stuck in one place. My code is

Class*
Class::GetP()
{
return ClassImpl->GetP();
}

In my ClassImpl->GetP() I have

ClassImpl*
ClassImpl::GetP()
{
return pClassImpl;
}

As you can see I need to convert my pImpl bact to my caller type. What is the way?

I dont want to use any casting
Please advice


Solution

  • Wouldn't you simply do

    Class *
    Class::GetP()
    {
        return this;
    }
    

    since callers of Class::GetP() shouldn't have a pointer to the internals anyway? (But then, what is that method for? It doesn't give the caller anything that they don't already have.)