在C ++类中向前声明typedef [英] Forward declare typedef within C++ class

查看:106
本文介绍了在C ++类中向前声明typedef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类中转发声明typedef的最佳解决方案是什么?以下是我需要解决的示例:

What's the best solution to forward declare a typedef within a class. Here's an example of what I need to solve:

class A;
class B;

class A
{
    typedef boost::shared_ptr<A> Ptr;

    B::Ptr foo();
};

class B
{
    typedef boost::shared_ptr<B> Ptr;

    A::Ptr bar();
};

我想我可以做以下事情:

I suppose I could just do the following:

boost::shared_ptr<B> foo();

但是还有更优雅的解决方案吗?

But is there a more elegant solution?

推荐答案

不幸的是,没有向前声明 typedef 这样的事情。但是,使用后期模板实例化有一个技巧:

There is no such thing as forward declaring a typedef unfortunately. However, there's a trick using late template instantiation:

template <typename T> class BImpl;

template <typename T>
class AImpl
{
public:
    typedef boost::shared_ptr<AImpl> Ptr;
    typename BImpl<T>::Ptr foo();
};

template <typename T>
class BImpl
{
public:
    typedef boost::shared_ptr<BImpl> Ptr;
    typename AImpl<T>::Ptr bar();
};

typedef AImpl<void> A;
typedef BImpl<void> B;

这有望完成您的目标。

This should hopefully accomplish the thing you're aiming for.

这篇关于在C ++类中向前声明typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆