const int*& vs typedef int* IntPtr

const int*& vs typedef int* IntPtr

本文关键字:int IntPtr typedef const vs      更新时间:2023-10-16

为什么我必须将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很有用,但不理解为什么需要它们。类CCD_ 3编译精细CCD_。

这是来自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 &);

相反,它应该编译并正常工作。