如何避免在实现中重复类名和模板调用

How to avoid repeating class name and template call in implementation?

本文关键字:调用 何避免 实现      更新时间:2023-10-16

我发现下面的代码非常难以阅读,我写了它!有没有

  1. 避免为每个实现的成员函数调用模板
  2. 避免为每个实现的成员函数设置ClassName::member_function_name?在这方面,我发现Java DRYer。您不会在任何地方重复类名。

谢谢!

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;
};
我认为在这种情况下

,您可以轻松地在声明中定义函数,并使用一些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;
};

这应该"几乎"等同于您的代码。"几乎",因为正如 xDD 所说,成员函数的体内定义隐式地将它们标记为内联。

默认情况下,类是私有的,结构默认是公共的。

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;
        }
};
既然

这是一个模板,为什么不在类体内定义成员函数呢?

无论如何,代码都需要在编译单元中可用以进行实例化,因此您将不会从将声明与定义分离中获得任何编译时间加速,并且编译器现在足够聪明,可以自行决定是否需要内联。