pimpl 习语和模板类朋友
我试图使用 pimpl 习惯用法来隐藏一些蹩脚的模板代码,但我无法让主体类友元的派生类访问句柄类。我从 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< 用户隐藏
interface::body
和 specific_body
实现/code> 如果从我的问题来看这并不明显。
I'm trying to use the pimpl idiom to hide some grungy template code, but I can't give derived classes of the body class friend access to the handle class. I get an error C2248 from MSVC 9 sp1. Here's some code to duplicate the error:
//
// 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;
}
Any help would be appreciated, I'm trying to hide interface::body
and specific_body
implementations from the users of interface
if that's not obvious from my question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要添加模板<>在模板测试方法的显式实例化中
You need to add template<> in the explicit instantiation of the template test method
您尚未获得
specific_body
资格。尝试作为你的朋友宣言。
You haven't qualified
specific_body
. Tryas your friend declaration.
尝试使用 typename 也许?我想我在 Sutter 中读到 typename 将能够进入未知范围内的类,而 class 则不会。
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.
除了 Troubadour 提到的不合格的 Specific_body 之外,您对 true_struct 和 false_struct 的 Specific_body<>::test 的专业化尝试似乎不正确。你必须专注于全班。
为了解决这个问题,我只需在公共部分声明body。另外,将 Specific_body 声明为 interface::body 的友元也没有帮助。
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.
好吧,我能够通过将
body
设为接口中的公共声明来“解决”这个问题。这解决了声明specific_body
期间的 C2248 错误。我还使body
成为interface
类的友元,并向body
结构添加了一个方法:以便生成
specific_body
如果specific_body
实例之间存在嵌套关系,code> 可以创建一个接口
Well, I was able to "solve" this problem by making the
body
a public declaration in the interface. That solves the C2248 error during the declaration of thespecific_body
. I also made thebody
a friend to theinterface
class and added a method to thebody
struct:so that a
specific_body
can create aninterface
if there is a nested relationship between instances ofspecific_body