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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)