如何转发声明应该属于类的模板化类型
How to forward declare templated type that should belong to a class?
假设我有两个类:
class A
{
public:
typedef std::shared_ptr<A> Ref;
...
private:
B::Ref _b;
}
class B
{
public:
typedef std::shared_ptr<B> Ref;
...
private:
A::Ref _a;
}
这显然需要B类和B::Ref。正向声明B很简单,但如何对B::Ref也这样做?
解决此问题的方法之一是
class A;
class B ;
template <typename T>
struct Traits {
typedef std::shared_ptr<T> Ptr;
};
class A
{
private:
Traits<B>::Ptr _b;
};
class B
{
private:
Traits<A>::Ptr _a;
};
不能正向声明嵌套的typedef
,因为在正向声明时B
将是一个不完整的类型。然而,您可以解决以下问题:
class B;
class A {
std::shared_ptr<B> _b;
public:
typedef std::shared_ptr<A> Ref;
};
class B {
A::Ref _a;
public:
typedef std::shared_ptr<B> Ref;
};
在类X
中有一个"这就是引用对象X
的方式"的typedef是一个糟糕的设计决定,正是因为您需要X
的完整定义来查看其成员,但您希望能够在没有其完整定义的情况下引用X
。
我能找到两种解决方法。一种是放弃作用域,简单地调用typedef RefA
,在类被前向声明的地方定义:
class A;
typedef std::shared_ptr<A> RefA;
或者,您可以将"知道如何引用"委托给一个单独的类。你可以把它作为一个类模板,这样类仍然可以在那里注册他们自己喜欢的引用类型:
template <class T>
struct RefT
{
typedef T *type; // Default reference type
};
template <class T>
using Ref = typename RefT<T>::type;
class A;
template <>
struct RefT<A>
{
typedef std::shared_ptr<A> type;
};
class B
{
private:
Ref<A> _a;
};
不幸的是,您不能正向声明嵌套的typedef。然而,您可以使用全局typedef,如
typedef std::shared_ptr<B> RefB ;
等等。另一种解决方案是使用后期模板专业化,比如以下内容:
template <typename T> class BImpl;
template <typename T>
class AImpl
{
public:
typedef std::shared_ptr<AImpl> Ref;
private:
typename BImpl<T>::Ref _b;
};
template <typename T>
class BImpl
{
public:
typedef std::shared_ptr<BImpl> Ref;
typename AImpl<T>::Ref _a;
};
typedef AImpl<void> A;
typedef BImpl<void> B;
当我想转发typedef时,我总是考虑继承。它可能看起来像这样:
template<typename T>
class Ref : public std::shared_ptr<T>
{
Ref()
{}
Ref(T *t)
: std::shared_ptr<T>(t)
{}
};
class B;
class A
{
public:
//...
private:
Ref<B> _b;
};
class B
{
public:
//...
private:
Ref<A> _a;
};
相关文章:
- 获取"rw_ssid"中成员"长度"的错误请求,该成员属于非类类型"char*"
- 隐式可转换参数,但属于引用类型
- 请求 中的成员,属于非类类型
- 查找树(不属于任何特定类型的简单连接树)中两个节点之间的路径
- 错误:请求 中的成员属于非类类型
- 请求在"伺服控制器"中请求成员"附加",该成员属于非类类型"int"
- 如何在C++中的另一个模板函数中使用属于模板化类的嵌套类型?
- 检查两种类型是否属于同一模板
- 仅当数组属于特定类型时,才将模板化参数添加到数组
- 检查第 n 个可变参数模板参数是否属于特定类型
- 请求 [..] 中的成员 [..],该成员属于非类类型 [..]
- Boost检查该类型是否属于给定类型的列表
- 错误:在“连接”中请求成员“板”,该“板”属于非类类型“游戏*”
- 私有结构(在类中定义)不能用作属于同一类的函数的返回类型吗
- C++ - 映射 - 错误:请求"w"中的成员'first',该成员属于非类类型 'const int'
- 当函数属于类时,未解析的重载函数类型
- 错误:请求“y”中的成员“x”,该成员属于非类类型“class**”
- 错误:请求"cur"中的成员"成员",该成员属于非类类型节点<int>* 二叉树
- 验证基对象是否属于特定的派生类型
- 键和值属于同一类型的多重映射是否有任何意义