Problem with C ++ Templates

I am trying to develop 2 classes, Node and Connection, but I have no experience with templates in C ++ or C ++.

Node contains a list of connections and a connection contains 2 nodes. Therefore, I believe that Node has a template parameter that indicates what type of connections are in the list, and that the connection has a template parameter that indicates which types of nodes it contains.

How can I force in C ++ that Node contains connections of a general type, but these connections contain nodes of the Node class? The same question for the Connection class. I want to have a generic parameter for the type of nodes, but these shared nodes should contain a list with connections of the Connection class.

I tried several things, this is what I have at the moment:

template <template <template <class Conn> class Node> class Conn>
class Node {
};

Can someone help me?

Thanks in advance,

Jeff

+3
1

, , - , ( - ), - :

template <class Node>
class Connection
{
    Node& node1;
    Node& node2;
};

template <class Node>
class NodeBase
{
    std::list< Connection<Node> > connections;
};

// example concrete node
class MassNode : public NodeBase<MassNode>
{
    // stuff that makes a mass node more than just a node.
}

, .

: ?

EDIT,

namespace intrusive
{
    template <class node>
    class directedConnection
    {
        node& From;
        node& To;
    };

    template <class node>
    class directedGraphNode
    {
    private:
        std::set< directedConnection<node>* > OutgoingConnections;
        std::set< directedConnection<node>* > IncomingConnections;
    };

    // sample concrete class. Note that it is a graph node AND it contains the node data.
    class bayesianNetworkNode : public directedGraphNode<bayesianNetworkNode>
    {
    public:
        double Probabilities[16];
    };

    bayesianNetworkNode B1, B2, B3;
}

namespace non_intrusive
{
    template <class T>
    class undirectedGraphNode;

    template <class T>
    class undirectedConnection
    {
        undirectedGraphNode<typename T>& Node1;
        undirectedGraphNode<typename T>& Node2;
    };

    template <class T>
    class undirectedGraphNode
    {
    private:
        std::set< undirectedConnection<T>* > Connections;
        T Value;
    public:
        T& operator * () { return Value; }
        T* operator -> () { return &Value; }
    };

    // sample concrete class. Note that this class contains the node data, but is NOT actually a graph node itself.
    // It is "pointed to" by a node in the same way that an STL iterator "points to" a collection item.
    class markovNetworkNode
    {
    public:
        std::set<formula> Formulae;
    };

    undirectedGraphNode<markovNetworkNode> M1, M2, M3;
}
+3

All Articles