Pimpl idiom
Peter Kümmel
syntheticpp at gmx.net
Sat Mar 4 09:25:05 GMT 2006
Marc Mutz wrote:
>> It is pure C++ and needs no macros or static variables.
>
> Yes, and since it uses no macros, it doesn't support the
> Derived::Private : public Base::Private
> optimization found in Qt4...
I've written a small test program
to see the problem you are talking about:
#include "KImpl.h"
#include <iostream>
namespace Private
{
int count = 1;
struct BImpl
{
BImpl()
{
std::cout << "BImpl created, " <<count<< "\n";
count++;
}
void foo(){std::cout<<"BImpl::foo()\n";}
};
struct CImpl : BImpl
{
CImpl() : BImpl()
{
std::cout<<"CImpl created \n";
}
void foo(){std::cout<<"CImpl::foo()\n";}
};
}
struct B
{
B()
{
d->foo();
}
KPimpl<Private::BImpl> d;
};
struct C : B
{
C() : B()
{
d->foo();
}
KPimpl<Private::CImpl> d;
};
int main()
{
C c;
system("pause");
return 0;
}
And the output is:
BImpl created, 1
BImpl::foo()
BImpl created, 2
CImpl created
CImpl::foo()
The "only" problem I see is the multiple creation of BImpl,
am I right?
Or are there more problems?
Maybe there is a macro-free way to handle this problem.
Peter
More information about the kde-core-devel
mailing list