2012-09-18 47 views
1

目前我有这一个排序功能:reinterpret_cast < type-id >的“type-id”是否是一个变量?

bool operator()(CVParent* lhs, CVParent* rhs) 
{ 
    double dFirstValue = reinterpret_cast< CVChild * >(lhs)->GetValue(m_lFeature); 
    double dSecondValue = reinterpret_cast< CVChild * >(rhs)->GetValue(m_lFeature); 
    .... 
} 

眼下型-ID被硬编码为CVChild *,但它可以是一个参数?我不想为CVParent的每个派生类写一个函数。

编辑:基于罗斯特的建议 我做了更改:

class Compare_Functor 
{ 
public: 

    Compare_Functor(const long& lFeature, const bool& bIsAscending) 
    { 
     m_lFeature = lFeature; 
     m_bIsAscending = bIsAscending; 
    } 

    template <class T> 
    bool operator()(CVParent* lhs, CVParent* rhs) 
    { 
     double dFirstValue = reinterpret_cast< T * >(lhs)->GetValue(m_lFeature); 
     double dSecondValue = reinterpret_cast< T * >(rhs)->GetValue(m_lFeature); 
     .... 
    } 

private: 

    long m_lFeature; 
    bool m_bIsAscending; 
} 

当前使用情况(怎么办修订了STL排序函数调用): 的std ::排序(m_pList,m_pList + getCount将( ),Compare_Functor(lFeature,TRUE));

我修复了代码。感谢大家的帮助!

template <class T> 
class Compare_Functor 
{ 
public: 

    Compare_Functor(const long& lFeature, const bool& bIsAscending) 
    { 
     m_lFeature = lFeature; 
     m_bIsAscending = bIsAscending; 
    } 

    bool operator()(CVParent* lhs, CVParent* rhs) 
    { 
     double dFirstValue = reinterpret_cast< T * >(lhs)->GetValue(m_lFeature); 
     double dSecondValue = reinterpret_cast< T * >(rhs)->GetValue(m_lFeature); 
     .... 
    } 

private: 

    long m_lFeature; 
    bool m_bIsAscending; 
} 


//Usage 
std::sort(m_pList, m_pList+GetCOunt(), Compare_Functor<CChild>(lFeature, TRUE)); 
+2

你可以给你的原因,你要使用'reinterpret_cast'? – evnu

+0

1. IIRC(我现在没有检查过)它可以是模板参数。 2.你确定你想要'reinterpret_cast'(在大多数情况下,演员不是最好的编程风格,除非真的需要,否则应该避开它们)? –

+0

不,我想使用reinterpret_cast,但传入的类型被转换为变量。 – AvatarBlue

回答

2

无法将任何动态(仅在运行时间中已知)类型传递到reinterpret_cast。它必须是静态的(在编译时已知)。

您可以使用模板,因为在其他的答案中提到,但你需要明确设置为投给每个函数调用的类型,因为编译器将无法从调用表达式推断出这一点:

template <class T> struct Functor 
{ 
    bool operator()(CVParent* lhs, CVParent* rhs) { ... } 
}; 

CVParent p1, p2; 
... 

// Usage 
Functor<CVChild1>().operator()(&p1, &p2); 
Functor<CVChild2>().operator()(&p1, &p2); 
Functor<CVChild3>().operator()(&p1, &p2); 
+0

谢谢你的帮助!我做了更改,但不知道如何实际传递子类的类型,因为我使用std :: sort来调用函子: – AvatarBlue

+0

std :: sort(m_pList,m_pList + GetCOunt(),Compare_Functor(lFeature, TRUE)); – AvatarBlue

+0

在这种情况下,您需要制作完整的仿函数模板,而不仅仅是operator()。我会编辑答案。 – Rost

1

您可以随时在您的实施中使用template

template <class Type> 
bool operator()(CVParent* lhs, CVParent* rhs) 
{ 
    double dFirstValue = reinterpret_cast< Type * >(lhs)->GetValue(m_lFeature); 
    double dSecondValue = reinterpret_cast< Type * >(rhs)->GetValue(m_lFeature); 
    .... 
} 
+0

+1了解和回答问题。 –

1

我会建议使用模板,但类模板,而不是功能模板。这将使其更自然地在标准库算法和contaiers使用:

template <typename T> 
struct CVFuntor 
{ 
    bool operator()(CVParent* lhs, CVParent* rhs) const 
    { 
    double dFirstValue = reinterpret_cast<T*>(lhs)->GetValue(m_lFeature); 
    double dSecondValue = reinterpret_cast<T*>(rhs)->GetValue(m_lFeature); 
    .... 
    } 
}; 

然后

typedef CVFunctor<CVChild> ParentToChild; 
typedef CVFunctor<CVOtherChild> ParentToOtherChild; 

.... 

ParentToChile p2ch; 
bool b = p2ch(SomeParentPtr1, SomeParentPtr2); 

你应该重新考虑你的reinterpret_cast使用。在我看来,要dynamic_cast选中的通话更适合在这里:

T* t = dynamic_cast<T*>(lhs); 
if (!t) return false;