c – const int *&vs typedef int * IntPtr
发布时间:2020-12-16 10:31:54 所属栏目:百科 来源:网络整理
导读:为什么我必须将int *更改为typedef int * IntPtr才能进行编译? template class Tclass 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
为什么我必须将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 &); 相反,它应该编译和工作得很好. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |