C++:类型定义和嵌套类问题

C++: typedefing and nested class issue

本文关键字:嵌套 问题 定义 类型 C++      更新时间:2023-10-16

我有:

class ThreadPool
{
public:
    ....
private:
    struct TP_Thread: public Thread_t
    {
        ....
    };
    std::vector<std::tr1::shared_ptr<TP_Thread> >   m_threads;
   .....
};

我想做一些类似的事情:

typedef std::tr1::shared_ptr<TP_Thread> shpThread;

以缩短类定义中的书写。问题是,我要么得到指向不完整类型的指针(因为类之前的前向声明和公共部分的typedef),要么试图访问ThreadPool的私有成员(在我试图在类之外对其进行typedef的情况下)。我如何对其进行typedef,以便在实现过程中自由使用它?

为什么不将typedef包含在类的公共部分中:

class ThreadPool
{
public:
    ....
private:
    struct TP_Thread: public Thread_t
    {
        ....
    };
public:
    typedef std::tr1::shared_ptr<TP_Thread> Shp;
    ...

然后在代码中使用ThreadPool::Shp