Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ directed graph node with template implementation
    primarykey
    data
    text
    <p>I am writing program which has a multitude of Directed Graph helper functions in order to gain a deeper understanding of C++. One of the central objects is called a Node which has member functions to help with calculating travel distance between nodes. I am trying to gain a better understanding of using C++ templates in OOP design. </p> <p>Here is a quick snapshot of the Node class</p> <pre><code>class Node { friend void swap(Node &amp; first, Node &amp; second) { using std::swap; swap(first.name, second.name); } public: Node(std::string val); Node(const Node &amp; copy); Node &amp; operator = (Node copy) { swap(*this, copy); return *this; } bool operator &lt; (Node &amp; rhs) const { return (size &lt; rhs.size); } bool operator &gt; (Node &amp; rhs) const { return (size &gt; rhs.size); } bool insertEdge(Node * dest, int distToNode); // I'd like for this return type to not be tied to an int // Especially if weights were represented as floats or doubles int findTravelDistance(Node * const &amp; toNode) const; int findTravelDistance(std::queue&lt;Node *&gt; * const &amp; nodeRoute) const; // Mutators void setNodeName(const std::string nameToSet); std::string getNodeName() const; void setNodeSize(const int size); int getNodeSize() const; // Misc void toString() const; // Constants static const bool ALLOW_CIRCULAR; ~Node(); protected: private: int size; std::string name; // Here int represents the weight of the edge. I would like it to be able to be // declared as an int, float, long, or double etc... std::map&lt;Node *, int&gt; * travelEdges; }; // end class } // end namespace </code></pre> <p>As I build this class to include more functionality, I find myself struggling with how to make my functions more adaptable. For instance, look at the findTravelDistance functions.</p> <p>What I would like to do is have the return type representing weight be type agnostic and the ordered map data structure's value to be type agnostic. As it is currently implemented, a user can only declare a type int for the weight. I realize I could embark upon function overloading. But, I feel this would be too redundant and an obvious violation of the DRY principle. If I were to have to change how this function works, I would have to change it for every overload. So my gut instinct tells me I should use C++ templates. Since I am new to templates, I am struggling with where to declare it. If I make my find functions template functions and just return the generic type..</p> <pre><code>template&lt;class T&gt; T findTravelDistance(std::queue&lt;Node *&gt; * const &amp; nodeRoute) const; </code></pre> <p>That will solve my problem there. But, it doesn't fix the issue where the underlying map data structure representing edges can only hold ints. My next thought was to declared a class template..</p> <pre><code>template&lt;class T&gt; class Node { ... } </code></pre> <p>But this also seemed odd to me. This would mean declaration and initialization would look something like</p> <pre><code>Node&lt;float&gt; * n = new Node&lt;float&gt;("N"); </code></pre> <p>If I were a user of my program, I would not immediately associate Node with the float type representing edge weights.</p> <p>So what is the best or appropriate usage of a template in this case? Or is using a template even the correct path here? It is possible my class design is flawed to begin with and not very C++'esk. Any feedback here is much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload