How to write a template?

試著忘記壹切 提交于 2019-12-25 01:55:10

问题


i need to write a template with Nodes containing data with 2 data structures : a map and a minimum heap, both got the same nodes in it and every 2 same nodes are connected. the problem is that i need the heap to know the node fields for the heapify for example, and i don't know what's the right way to do so, friends? public fields in Node? writing the Node inside the heap? using getters and setters? thanks all for your help.


回答1:


Well, a linked list might be laid out like this:

namespace my_namespace
{
namespace detail 
{
template <class T>
struct Node
{
    T value;
    Node* previous;
    Node* next;
    //constructors and other things that might help
};
}

template <class T>
class LinkedList
{
private:
    detail::Node<T>* head;
public:
    //all it does    
};
}

There is no particular reason to hide the Node struct from the user or the LinkedList class (putting it into a detail namespace should be more than enough): LinkedList needs it and the Node itself is pretty much useless to the user. All encapsulation is up to LinkedList to achieve: it just shouldn't give out it's head (or any other Node*).



来源:https://stackoverflow.com/questions/3745412/how-to-write-a-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!