关于PIMPL语法
我对PIMPL语法中使用的C ++使用情况有疑问。
首先,为什么不需要编写pimpl(new Impl)
为pimpl(new my_class :: :: inprand)
第二,为什么new new Impl < /代码>扩展即使是临时对象?
//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 ...
}
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 ...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先:在您的构造函数中表达式新的imph 确实是一个临时对象,但是立即将其传递给
pimpl
成员,这不是临时。std :: unique_ptr
类型存储指针传递给它,并将其保留在其上,直到被破坏或移动为止。直到my_class
对象被破坏之前,成员不会被破坏。另外,考虑使用
std :: make_unique
而不是new
。第二:您不必写
my_class :: impl
因为您处于my_class
的成员函数,因此名称分辨率看起来看起来不仅在命名空间范围内,而且在类范围范围内。因此,impl
可以解决。First: In your constructor the expression
new impl
indeed is a temporary object, but it is immediately passed to thepimpl
member which is not a temporary. Thestd::unique_ptr
type stores the pointer passed to it and holds on to it until it is destroyed or moved. And the member is not destroyed until themy_class
object is destroyed.Also, consider using
std::make_unique
instead ofnew
.Second: You don't have to write
my_class::impl
because you are in a member function ofmy_class
, so name resolution looks not only at namespace scope but also at class scope. So,impl
can be resolved.范围。当定义构造函数时,您在班级的范围内,因此 name lookup 能够毫无问题地找到它。
您将临时指针传递给
std :: simolor_ptr&lt; nlpt&gt;
的构造函数参数,因此值pimpl
消耗了值。impl
值s不是临时的,因为它是在堆上创建的,对其寿命的控制传递给了智能指针。额外:
您在标题中使用命名空间std; 使用
(唯一可以跳过
std ::
之前simele_ptr
之前的情况)!这是一个大错误,不要这样做。这将影响所有将包括此标头的所有来源。因此,您可能会有符号歧义错误。即使在CPP文件中使用命名空间std; 使用
也是考虑了不良练习。
Scope. When constructor is defined you are inside a cope of a class, so name lookup is able to find it without problems.
You are passing temporary pointer to as constructor argument of
std::unique_ptr<impl>
so value is consumed by a fieldpimpl
.impl
value s not temporary since it is created on a heap, control of its lifetime was passed to smart pointer.Extra:
You did
using namespace std;
in header (the only case when you can skipstd::
beforeunique_ptr
)! This is BIG mistake do not do it. This will impact dangerously all sources which will include this header. You can have symbol ambiguity error because of that.Even having
using namespace std;
in cpp file is considered a bad practice.新的Impl
不是临时的,因为它不是变量。这是一个表达式,
my_class :: pimpl
对象成员构建过程中进行评估,impl
对象(其寿命是由控制的,而不是自动的,因为它不是本地自动变量),inpher
对象。该表达式用作
pimpl
成员my_class
要构造的对象的初始化器,因此返回的指针作为初始化值传递给pimpl
并存储在其中。这允许您的my_class
对象控制impl
对象的寿命。pimpl
指针变量的寿命显然与其拥有my_class
对象的寿命相同,但是指向inpers> impl
对象的寿命取决于您的my_class
逻辑:replace> replast()
与另一个随意pimpl
,delete
impl
对象,并且在不再需要的时候无需任何工作The
new impl
is not temporary because it's not a variable.It is an expression, which
my_class::pimpl
object member construction,impl
object (whose lifetime is controlled, not automatic, because it's not a local automatic variable),impl
object.The expression is used as an initializer to the
pimpl
member of themy_class
object being constructed, so the returned pointer is passed as an initializing value topimpl
and gets stored in it. This allows yourmy_class
object to control the life time of theimpl
object.The life time of the
pimpl
pointer variable is obviously the same as its owningmy_class
object's life time, however the life time of the pointedimpl
object depends on yourmy_class
logic:replace()
it with another one at willrelease()
thepimpl
,delete
theimpl
object and work without any when no longer necessary...