I have a question about the C++ usage used in the pimpl syntax.
First, why is it not necessary to write pimpl( new impl )
as pimpl( new my_class::impl )
Second, why is the lifetime of new impl
extended even though it is a temporary object?
//my_class.h
class my_class {
// ... all public and protected stuff goes here ...
private:
class impl; unique_ptr<impl> pimpl; // opaque type here
}
// my_class.cpp
class my_class::impl { // defined privately here
// ... all private data and functions: all of these
// can now change without recompiling callers ...
};
my_class::my_class(): pimpl( new impl )
{
// ... set impl values ...
}