读书人

关于模板的typedef有关问题

发布时间: 2013-04-02 12:35:26 作者: rapoo

关于模板的typedef问题,
先看代码:


class TestBase
{
public:
virtual ~TestBase() {}

virtual void FuncA(int param) = 0;
virtual bool FuncB(int param) = 0;
};

template<typename T>
class TestA: public TestBase
{
public:
typedef void (T::*AFuncType)(int param);
typedef bool (T::*BFuncType)(int param);

public:
TestA(T * Obj, AFuncType AFunc, BFuncType BFunc): m_Obj(Obj), m_FuncA(AFunc), m_FuncB(BFunc) {}

~TestA(){}

virtual void FuncA(int param)
{
return (m_Obj->*m_FuncA)(param);
}

virtual bool FuncB(int param)
{
return (m_Obj->*m_FuncB)(param);
}

private:

T * m_Obj;
AFuncType m_FuncA;
BFuncType m_FuncB;
};


class Manager
{
public:
// template<typename T>
// Manager(T Obj, typename TestA<T>::FuncAType AFunc, typename TestA<T>::FuncBType BFunc):m_TestBase(new TestA(Obj, AFunc, BFunc))
// {
//
// }

template<typename T>
Manager(T* Obj, void (T::*AFunc)(int Param), bool (T::*BFunc)(int Param)):m_TestBase(new TestA<T>(Obj, AFunc, BFunc))
{

}

~Manager()
{
delete m_TestBase;
}

void FuncA(int param)
{
return m_TestBase->FuncA(param);
}

bool FuncB(int param)
{
return m_TestBase->FuncB(param);
}

private:
TestBase *m_TestBase;
};


class MyClass1
{
public:
void Set(int param)
{

}

bool Check(int param)
{
return false;
}
private:
doublem_value;
};


class MyClass2
{
public:
void Set(int param)
{

}

bool Check(int param)
{
return false;
}

private:
intm_value;
};

int main()
{
MyClass1 mc1;
MyClass2 mc2;

Manager mgr1(&mc1, &MyClass1::Set, &MyClass1::Check);

Manager mgr2(&mc2, &MyClass2::Set, &MyClass2::Check);

return 0;
}

如果把用注释中的的typedef类型,则编译会出现,构造函数不接受3个参数的提示
如果不用typedef,就没有问题。
求高手解答 template;?typedef;
[解决办法]
引用:
先看代码:
C/C++ code?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889……


class TestBase
{
public:
virtual ~TestBase() {}

virtual void FuncA(int param) = 0;
virtual bool FuncB(int param) = 0;
};

template<typename T>
class TestA: public TestBase
{
public:
typedef void (T::*AFuncType)(int param);
typedef bool (T::*BFuncType)(int param);
public:
TestA(T* Obj, AFuncType AFunc, BFuncType BFunc)


: m_Obj(Obj), m_FuncA(AFunc), m_FuncB(BFunc) {}
~TestA(){}

virtual void FuncA(int param)
{
// void函数不要返回值
//return (m_Obj->*m_FuncA)(param);
(m_Obj->*m_FuncA)(param);
}

virtual bool FuncB(int param) { return (m_Obj->*m_FuncB)(param); }
private:
T * m_Obj;
AFuncType m_FuncA;
BFuncType m_FuncB;
};

class Manager
{
public:
// template<typename T>
// Manager(T Obj, typename TestA<T>::FuncAType AFunc, typename TestA<T>::FuncBType BFunc):m_TestBase(new TestA(Obj, AFunc, BFunc))
// {
// }

// 参数类型中,嵌套类型名称都写错了,肯定找不到
// 另外,TestA要显示写成TestA<T>
// 其次,Manager的第一个参数是个指针(看你下面的调用形式),你这里写成值了,
// 那么T被推导为MyClass1*和MyClass2*类型,而指针肯定不存在嵌套的AFuncType和BFuncType类型
template<typename T>
Manager(T* Obj, typename TestA<T>::AFuncType AFunc,
typename TestA<T>::BFuncType BFunc):m_TestBase(new TestA<T>(Obj, AFunc, BFunc))
{
}

//template<typename T>
//Manager(T* Obj, void (T::*AFunc)(int Param), bool (T::*BFunc)(int Param)):m_TestBase(new TestA<T>(Obj, AFunc, BFunc))
//{
//}

~Manager() { delete m_TestBase; }

void FuncA(int param) { return m_TestBase->FuncA(param); }
bool FuncB(int param) { return m_TestBase->FuncB(param); }
private:
TestBase *m_TestBase;
};

class MyClass1
{
public:
void Set(int param) { }
bool Check(int param) { return false; }
private:
doublem_value;
};

class MyClass2
{
public:
void Set(int param) { }
bool Check(int param) { return false; }
private:
intm_value;
};

int main()
{
MyClass1 mc1;
MyClass2 mc2;

Manager mgr1(&mc1, &MyClass1::Set, &MyClass1::Check);
Manager mgr2(&mc2, &MyClass2::Set, &MyClass2::Check);
return 0;
}

读书人网 >C++

热点推荐