C++中模板的某种用法
阿神
阿神 2017-04-17 14:29:36
0
1
361

看一些代码看到源码中模板的写法:

// 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>

模板的第二个参数是什么用法,没搞懂什么意思..
没搞懂模板这种用法的目的是啥

阿神
阿神

闭关修行中......

reply all(1)
Peter_Zhu

It seems that the second parameter DoublyLinkedListLink<Element> Element::* LinkMember is a pointer to the member in Element. If you think about it this way, it means that in the Element type, there is a member whose type is DoublyLinkedListLink<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...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template