简单示例:
struct Bar {
// some data that we want to point to
};
struct Foo {
Bar bar;
};
shared_ptr<Foo> f = make_shared<Foo>(some, args, here);
shared_ptr<Bar> specific_data(f, &f->bar);
// ref count of the object pointed to by f is 2
f.reset();
// the Foo still exists (ref cnt == 1)
// so our Bar pointer is still valid, and we can use it for stuff
some_func_that_takes_bar(specific_data);
别名是指当我们真的想指向
Bar
,但我们也不想
Foo
正如Johannes在评论中指出的那样,有一个相当的语言特征:
Bar const& specific_data = Foo(...).bar;
Bar&& also_specific_data = Foo(...).bar;
我们指的是临时组织的成员,但是临时组织
只要你还活着
specific_data
shared_ptr
例如,我们有一个
他的一生与
福
-a