【发布时间】:2017-01-19 23:29:03
【问题描述】:
我有一个如下所示的实现:
class A : public std::enable_shared_from_this<A>
{
public:
A() {}
void dummy(std::string name);
private:
std::map<std::string, std::string> cache;
};
void
A::dummy(std::string name) {
auto shared_this = shared_from_this();
auto find =
[name, shared_this] () {
auto iter = shared_this->cache.find(name);
};
}
我不确定这条线是如何工作的: auto iter = shared_this->cache.find(name);
看起来我们正在尝试使用指向类的指针来访问私有成员,但我不确定它是否会有所不同。
这是如何工作的?
【问题讨论】:
-
这没问题。由于
dummy()是A类的成员,它可以访问A的任何实例的任何私有成员。
标签: c++ memory-management this shared-ptr