How to avoid repeating the class name and calling the template in the implementation?

I find the code below terribly difficult to read, and I wrote it! Is there anything for

  • Avoid calling a template for each member function implemented.
  • avoid ClassName::member_function_namefor every member function implemented? I find Java DRYer in this regard. You do not repeat the class name everywhere.

Thank!

template <class KeyType, class ObjectType>
class Vertex
{
private:
    KeyType key;
    const ObjectType* object;
public:
    Vertex(const KeyType& key, const ObjectType& object);
    const KeyType getKey();
};

template <class KeyType, class ObjectType> 
class Graph
{
private:
    map<KeyType, Vertex<KeyType, ObjectType> > vertexes;
public:
    const Vertex<KeyType, ObjectType>& createVertex(const KeyType& key, const ObjectType& object);
};

template <class KeyType, class ObjectType>
Vertex<KeyType, ObjectType>::Vertex(const KeyType& objectKey, const ObjectType& newObject)
{
    key = objectKey;
    object = &newObject;
};

template <class KeyType, class ObjectType>
const KeyType Vertex<KeyType, ObjectType>::getKey()
{
    return key;
};

template <class KeyType, class ObjectType>
const Vertex<KeyType, ObjectType>& Graph<KeyType, ObjectType>::createVertex(const KeyType& key, const ObjectType& object)
{
    Vertex<KeyType, ObjectType> *vertex = new Vertex<KeyType, ObjectType>(key, object);
    vertexes.insert(make_pair(vertex->getKey(), *vertex));
    return *vertex;
};
+5
source share
3 answers

If this is a template, why not define member functions right inside the class body?

, , , , , .

+1

. "", , xDD, - .

, Struct .

template <class KeyType, class ObjectType>
class Vertex
{
    KeyType key;
    const ObjectType* object;

    public:
        Vertex(const KeyType& _key, const ObjectType& _object) : key(_key), object(&_object) {}

        const KeyType getKey()
        {
            return key;
        }
};

template <class KeyType, class ObjectType> 
class Graph
{
    map<KeyType, Vertex<KeyType, ObjectType> > vertexes;

    public:
        const Vertex<KeyType, ObjectType>& createVertex(const KeyType& key, const ObjectType& object)
        {
            Vertex<KeyType, ObjectType> *vertex = new Vertex<KeyType, ObjectType>(key, object);
            vertexes.insert(make_pair(vertex->getKey(), *vertex));
            return *vertex;
        }
};

typedef:

template <class KeyType, class ObjectType>
class Vertex
{
    KeyType key;
    const ObjectType* object;

    public:
        Vertex(const KeyType& _key, const ObjectType& _object) : key(_key), object(&_object) {}
        const KeyType getKey()
        {
            return key;
        }
};

template <class KeyType, class ObjectType> 
class Graph
{
    typedef Vertex<KeyType, ObjectType> tVertex;
    map<KeyType, tVertex > vertexes;

    public:
        const tVertex& createVertex(const KeyType& key, const ObjectType& object)
        {
            tVertex *vertex = new tVertex(key, object);
            vertexes.insert(make_pair(vertex->getKey(), *vertex));
            return *vertex;
        }
};
+1

, typedefs .

template <class KeyType, class ObjectType>
class Vertex {
  public:
    Vertex(const KeyType& key, const ObjectType& object) :
           key(objectKey), object(&newObject) { };
    const KeyType getKey() const { return key; };
  private:
    KeyType key;
    const ObjectType* object;
};

template <class KeyType, class ObjectType> 
class Graph {
  public:
    typedef Vertex<KeyType, ObjectType> vertex_type;

    const vertex_type& createVertex(const KeyType& key, const ObjectType& object) {
      vertex_type* vertex = new vertex_type(key, object);
      vertexes.insert(make_pair(vertex->getKey(), *vertex));
      return *vertex;
    };
  private:
    map<KeyType, vertex_type > vertexes;
};
+1

All Articles