I am trying to break some code from my C ++ modeling software into a library, so it can be used more flexibly. The simulation is based on a Lattice , consisting of a number of Node , which contain lists of pointers to their Neighbor s. Although neighbors are also nodes, I would like to have a small wrapper class around the *Node pointer to implement additional logic / fields (e.g. bool is_neares_neighbor or so on.
The architecture of my class therefore looks something like this:
class Lattice { private: vector<Node> _nodes; }; class Node { private: vector<Neighbor> _neighbors; }; class Neighbor { private: Node * _node; };
So far so good. Now I want my library to handle all the lattice-related logic, but nothing more. However, when using the library in some project, three classes ( Lattice , Node , Neighbor ) will carry more logic and fields. Thus, the user must inherit these classes and implement their custom materials, while the library still handles all the necessary lattice-related logic.
What is the recommended way to do this? Are the patterns consistent? In a template situation, my class hierarchy will look like this:
template<class N> class Lattice { private: vector<N> _nodes; }; template<class NN> class Node { private: vector<NN> _neighbors; }; template<class N> class Neighbor { private: N * _node; };
As you can see, both Node and Neighbor must know each other's type, which is a circular condition that I don’t know how to deal with. In addition, the entire library will have to live in the header files.
How are situations like this in the C ++ world in the most elegant way?
source share