访问模板基类函数指针类型
我有一门课,我真的不想改变,但我确实想延长。 我是一名模式和模板新手,正在尝试将装饰器模式应用于模板类。 模板类在另一个类中包含一个指向成员的指针(如果我正确理解语义的话)。 指向成员的指针是 XML istream 的反序列化器。 类型“T”是要反序列化的 XML 文档的类型。
template <typename T> class B {
public:
typedef std::auto_ptr<T> MYFUN(
std::istream&, const std::string&, const std::string& );
public:
B<T>( MYFUN* p );
private:
MYFUN *fptr;
std::string aString1;
std::string aString2;
};
阅读 http: 后,typedef 对我来说看起来很奇怪//www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.5,但这个类似乎按原样工作得很好。 提供的头文件中没有任何额外的#defines,所以这对我来说有点神秘。
现在我尝试将它扩展为装饰器,因为我想对 MYFUN 返回的 auto_ptr 对象做更多的工作:
template <typename T>
class D : public class B<T>
{
D( B<T>::MYFUN *fPtr, B<T> *providedBase ); //compiler complaint
//Looks like B
private:
B* base_;
};
template <typename T>
D<T>::D( B<T>::MYFUN *p, B<T> *base ) //compiler complaint
:
B<T>::B( p ), base_(providedBase)
{ }
当尝试编译它时,我在显示的两行处收到语法投诉。 错误类似于“* 处预期的 ')'”。 没有人抱怨 MYFUN 未定义。
当我使用与 D 中相同的签名重新定义 D 中的成员指针时,即
//change MYFUN to NEWFUN in D)
typedef std::auto_ptr<T> MYNEWFUN(
std::istream&, const std::string&, const std::string& );
这有效。 我不想对 B 中的每个 D/Decorator 都执行此操作。我尝试更全局地执行 typedef,但由于模板参数未定义,无法获得正确的语法。
I have a class that I've been provided that I really don't want to change, but I do want to extend. I'm a pattern and template newbie experimenting with a Decorator pattern applied to a template class. Template class contains a pointer-to-member (if I understand the semantics correctly) in yet another class. The pointer-to-member is the deserializer of an XML istream. The type 'T' is the type of XML document to be deserialized.
template <typename T> class B {
public:
typedef std::auto_ptr<T> MYFUN(
std::istream&, const std::string&, const std::string& );
public:
B<T>( MYFUN* p );
private:
MYFUN *fptr;
std::string aString1;
std::string aString2;
};
The typedef looks odd to me after reading http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.5, and yet this class seems to work fine as-is. There aren't any additional #defines in the provided header file, so this is a little mysterious to me.
Now I try to extend it, as Decorator because I want to do a bit more work on the auto_ptr object returned by MYFUN:
template <typename T>
class D : public class B<T>
{
D( B<T>::MYFUN *fPtr, B<T> *providedBase ); //compiler complaint
//Looks like B
private:
B* base_;
};
template <typename T>
D<T>::D( B<T>::MYFUN *p, B<T> *base ) //compiler complaint
:
B<T>::B( p ), base_(providedBase)
{ }
When trying to compile this, I get a syntax complaint at the two lines shown. Error is something like "expected ')' at *". There is no complaint about MYFUN being undefined.
When I re-define the pointer-to-member in D with the same signature as in D, i.e.
//change MYFUN to NEWFUN in D)
typedef std::auto_ptr<T> MYNEWFUN(
std::istream&, const std::string&, const std::string& );
This works. I prefer not to have to do this for every D/Decorator I might make of B. I tried to perform the typedef more globally, but couldn't get the syntax right due to the template parameter being undefined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
B 中 MYFUN typedef 的定义是通过私有可见性完成的。 D 将无法访问它。 如果您将其更改为受保护或公共,那么它可以工作吗?
The definition of the MYFUN typedef in B is done with private visibility. D will not be able to access it. If you change it to protected or public, does it then work?
编译错误是由于编译器无法告诉您正在谈论一种类型。
尝试:
并
查看:C++ FAQ Lite 的模板部分< /a> 了解有关为什么需要这样做的更多详细信息,但总结是,由于模板专门化的可能性,编译器无法确保
B::MYFUN
实际上指的是一种类型。The compile error is due to the fact that the compiler can't tell you are talking about a type.
Try:
and
See: the templates section of the C++ FAQ Lite for more details on why this is necessary, but the summary is that because of the possibility of template specialization, there is no way for the compiler to be sure that
B<T>::MYFUN
is actually referring to a type.我看到的问题是 B::MYFUN 是一个 private typedef。
因此,任何继承类都无法访问它。
将其更改为:
The problem I see with this is that B::MYFUN is a private typedef.
Therefore, any inheriting class cannot access it.
Change this to: