【发布时间】:2014-09-30 14:57:19
【问题描述】:
我有一个问题。我在玩enable_shared_from_this 并注意到一件奇怪的事情。这个例子很好用:
#include <iostream>
#include <memory>
using namespace std;
struct Test : enable_shared_from_this<Test>
{
};
int main() {
shared_ptr<Test> ptr(new Test);
return 0;
}
但是当我将struct 更改为class 时,它会停止编译!
错误提示:
/usr/include/c++/4.8/bits/shared_ptr_base.h:772:58:错误: ‘std::enable_shared_from_this’ 是 ‘Test’ 的一个不可访问的基础 __enable_shared_from_this_helper(_M_refcount, __p, __p);有人知道为什么会这样吗?
【问题讨论】:
-
struct的默认继承是public,而class的默认继承是private。 -
对。默认继承就是答案。谢谢!
标签: c++ c++11 shared-ptr private-inheritance