c – const int *&vs typedef int * IntPtr

c – const int *&vs typedef int * IntPtr,第1张

概述为什么我必须将int *更改为typedef int * IntPtr才能进行编译? template <class T>class A{ public: template <class X> void a(X *x, void (X::*fun)(const T&)) { }};typedef int * IntP 为什么我必须将int *更改为typedef int * IntPtr才能进行编译?

template <class T>class A{    public:        template <class X>        voID a(X *x,voID (X::*fun)(const T&))        {        }};typedef int * IntPtr;class B{    public:        B() : a()        {            a.a(this,&B::foo); // this won't work        }        voID foo(const int *&) // must replace `int *` here with `IntPtr`        {        }        A<int *> a; // ...and here};class C{    public:        C() : a()        {            a.a(this,&C::foo);        }        voID foo(const IntPtr&)        {        }        A<IntPtr> a;};

我理解为什么typedef是有用的,但不是为什么它们是必需的. C类编译精细B没有.

这是MSVC 2008编译器的错误:

Error   1   error C2784: 'voID A<T>::a(X *,voID (__thiscall X::* )(const T &))' : Could not deduce template argument for 'voID (__thiscall X::* )(const T &)' from 'voID (__thiscall B::* )(const int *&)'
解决方法 const int *&和typedef int * IntPtr; const IntPtr&不一样.在第一种情况下,它是int常量,在第二种情况下它是指针.只有第二种情况与您的模板兼容.

如果你写

voID foo(int * const &);

相反,它应该编译和工作得很好.

总结

以上是内存溢出为你收集整理的c – const int *&vs typedef int * IntPtr全部内容,希望文章能够帮你解决c – const int *&vs typedef int * IntPtr所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1220863.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存