如何在C++模板类之间共享受保护的成员
How to share protected members between C++ template classes?
考虑以下示例:
class _ref
{
public:
_ref() {}
_ref(const _ref& that) {}
virtual ~_ref() = 0;
};
_ref::~_ref() {}
template <typename T>
class ref : public _ref
{
protected:
ref(const _ref& that) {}
public:
ref() {}
ref(const ref<T>& that) {}
virtual ~ref() {}
template <typename U>
ref<U> tryCast()
{
bool valid;
//perform some check to make sure the conversion is valid
if (valid)
return ref<U>(*this); //ref<T> cannot access protected constructor declared in class ref<U>
else
return ref<U>();
}
};
我希望所有类型的"ref"对象都能够访问彼此受保护的构造函数。有什么办法可以做到这一点吗?
template <typename T>
class ref : public _ref
{
template <typename U>
friend class ref;
//...
DEMO
相关文章:
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- C++两个源文件之间共享的枚举的静态实例
- 线程之间的实时数据共享
- 共享库 (.so) 没有扩展名的 Linux 可执行文件之间的区别?
- 内联函数的函数本地静态对象是否在共享对象文件之间共享?
- Directx 12 :在两个进程之间共享图形内存
- 子目录之间的 CMAKE 共享变量
- C++ 多个 DLL 之间的共享接口
- 如何在进程之间共享大量数据而不重复?(国际刑罚委员会)
- 简单使用 std::atomic 在两个线程之间共享数据
- C++:在另外两个对象之间共享一个对象
- 在C++中的程序之间共享指针
- 拥有或在对象之间共享资源
- 在两个结构之间共享数组
- 如何在不同的CMakeList.txt之间共享变量?
- 在QT中的两个窗口之间共享websocket连接
- 在VS2017中,我们如何在项目成员之间共享编译设置
- 通过require在不同的Lua状态之间共享全局变量