看一些代码看到源码中模板的写法:
// DoublyLinkedListLink
template<typename Element>
class DoublyLinkedListLink {
public:
Element* next;
Element* previous;
};
// DoublyLinkedListMemberGetLink
template<typename Element,
DoublyLinkedListLink<Element> Element::* LinkMember = &Element::fLink>
class DoublyLinkedListMemberGetLink {
private:
typedef DoublyLinkedListLink<Element> Link;
public:
inline Link* operator()(Element* element) const
{
return &(element->*LinkMember);
}
inline const Link* operator()(const Element* element) const
{
return &(element->*LinkMember);
}
};
这行代码:
template<typename Element,
DoublyLinkedListLink<Element> Element::* LinkMember = &Element::fLink>
模板的第二个参数是什么用法,没搞懂什么意思..
没搞懂模板这种用法的目的是啥
It seems that the second parameter
DoublyLinkedListLink<Element> Element::* LinkMember
is a pointer to the member inElement
. If you think about it this way, it means that in theElement
type, there is a member whose type isDoublyLinkedListLink<Element>
. This statement means It means declaring a member pointer type and assigning it a default parameter&Element::fLink
. I found a somewhat similar question on stackoverflow. Please check it out. I hope it will be helpful to you http://stackoverflow.com/ques...