访问模板基类函数指针类型

发布于 2024-07-19 08:39:29 字数 1531 浏览 6 评论 0原文

我有一门课,我真的不想改变,但我确实想延长。 我是一名模式和模板新手,正在尝试将装饰器模式应用于模板类。 模板类在另一个类中包含一个指向成员的指针(如果我正确理解语义的话)。 指向成员的指针是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好听的两个字的网名 2024-07-26 08:39:29

B 中 MYFUN typedef 的定义是通过私有可见性完成的。 D 将无法访问它。 如果您将其更改为受保护或公共,那么它可以工作吗?

template <typename T> class B {
  protected:
    typedef std::auto_ptr<T> MYFUN( 
      std::istream&, const std::string&, const std::string& );
...
};

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?

template <typename T> class B {
  protected:
    typedef std::auto_ptr<T> MYFUN( 
      std::istream&, const std::string&, const std::string& );
...
};
奢欲 2024-07-26 08:39:29

编译错误是由于编译器无法告诉您正在谈论一种类型。

尝试:

D( typename B<T>::MYFUN *fPtr, B<T> *providedBase );

template <typename T>
D<T>::D( typename B<T>::MYFUN *p, B<T> *base )

查看: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:

D( typename B<T>::MYFUN *fPtr, B<T> *providedBase );

and

template <typename T>
D<T>::D( typename B<T>::MYFUN *p, B<T> *base )

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.

哎呦我呸! 2024-07-26 08:39:29

我看到的问题是 B::MYFUN 是一个 private typedef。

因此,任何继承类都无法访问它。

将其更改为:

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 problem I see with this is that B::MYFUN is a private typedef.

Therefore, any inheriting class cannot access it.

Change this to:

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;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文