1

我正在尝试使用 pimpl 习惯用法来隐藏一些蹩脚的模板代码,但我不能让 body 类的派生类访问句柄类。我从 MSVC 9 sp1 收到错误 C2248。这是一些复制错误的代码:

//
// interface.hpp
//
namespace internal{
    template<class T>
    class specific_body;
}

class interface
{
    struct body;
    body *pbody_;
    interface(body *pbody);

    template<class T>
    friend class internal::specific_body;

public:

    ~interface();

    interface(const interface &rhs);

    bool test() const;

    static interface create( bool value );
};

//
// interface.cpp
//
struct interface::body
{
    virtual ~body(){}

    virtual bool test() const = 0;

    virtual interface::body *clone() const = 0;
};

class true_struct {};
class false_struct {};

namespace internal {

template< class T>
class specific_body : public interface::body
{ // C2248
public:

    specific_body(){}

    virtual bool test() const;

    virtual interface::body *clone() const
    {
        return new specific_body();
    }
};

bool specific_body<true_struct>::test() const
{
    return true;
}

bool specific_body<false_struct>::test() const
{
    return false;
}

} //namespace internal

interface::interface(body *pbody) : pbody_(pbody) {}

interface::interface(const interface &rhs) : pbody_(rhs.pbody_->clone()) {}

interface::~interface() { delete pbody_; }

bool interface::test() const
{
    return pbody_->test();
}

interface interface::create(bool value )
{
    if ( value )
    {
        return interface(new internal::specific_body<true_struct>());
    }
    else
    {
        return interface(new internal::specific_body<false_struct>());
    }
}

//
// main.cpp
//
// #include "interface.hpp"
//

int _tmain(int argc, _TCHAR* argv[])
{
    interface object( interface::create(true));

    if ( object.test() )
    {
        // blah
    }
    else
    {
    }
    return 0;
}

任何帮助将不胜感激,我试图向用户隐藏interface::bodyspecific_body实施,interface如果这在我的问题中并不明显。

4

5 回答 5

1

您需要在模板测试方法的显式实例化中添加 template<>

template<> // add this line
bool specific_body<true_struct>::test() const
{
    return true;
}
于 2009-10-17T09:26:09.730 回答
0

Try using typename maybe? I think I read in Sutter that typename will work to get to class inside of an unknown scope, while class won't.

于 2009-10-16T18:29:42.900 回答
0

In addition to the unqualified specific_body mentioned by Troubadour, your specialization attempt of specific_body<>::test for true_struct and false_struct seems incorrect. You have to specialice the full class.

To solve the problem, I'd simply declare body in the public section. Declaring specific_body to be a friend of interface::body in addition doesn't help either.

于 2009-10-16T18:35:44.483 回答
0

你没有资格specific_body。尝试

template<class T>
friend class internal::specific_body;

作为你的朋友声明。

于 2009-10-16T18:14:50.580 回答
0

好吧,我能够通过body在界面中进行公共声明来“解决”这个问题。这解决了specific_body. 我还让body这个班级成为朋友,interface并向结构添加了一个方法body

static interface create( body *pbody )
{
    return interface(pbody);
}

这样如果 a 的实例之间存在嵌套关系,则aspecific_body可以创建interfacespecific_body

于 2009-10-19T11:35:25.383 回答