Forward declare typedef within C++ class

前端 未结 5 707
长发绾君心
长发绾君心 2021-02-04 06:56

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 boos         


        
5条回答
  •  隐瞒了意图╮
    2021-02-04 07:18

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

    template  class BImpl;
    
    template 
    class AImpl
    {
    public:
        typedef boost::shared_ptr Ptr;
        typename BImpl::Ptr foo();
    };
    
    template 
    class BImpl
    {
    public:
        typedef boost::shared_ptr Ptr;
        typename AImpl::Ptr bar();
    };
    
    typedef AImpl A;
    typedef BImpl B;
    

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

提交回复
热议问题