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

问题描述:

我发现下面的代码非常难以阅读,我写了!有没有到如何避免在实现中重复类名和模板调用?

  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; 
}; 
+2

只是一个小提示:我经常发现类更容易使用的公共接口读宣布第一 – jeffythedragonslayer 2011-04-05 00:53:56

+0

“美在旁观者的眼睛”我看没有什么吸引力的代码。 – 2011-04-05 01:08:18

+0

您会发现typedefs有助于简化代码:例如,可以在很多地方使用'Vertex '。 – 2011-04-05 01:09:15

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

代码需要在编译单元中用于实例化,所以你不会获得任何编译时加速从分离声明和定义,编译器现在足够聪明以决定是否需要内联。

这应该是“几乎”相当于您的代码。 “差不多”,因为正如xDD所说,成员函数的in-body定义将其隐含地标记为内联。

默认情况下,这个类是私有的,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; 
     } 
}; 
+0

由于成员函数的体内定义将它们隐式标记为内联,因此不是等同的。 – xDD 2011-04-05 01:05:38

+0

@xDD:你说得对,我会编辑。 – Valkea 2011-04-05 01:08:14

+0

@xDD:我怀疑有问题;它是否也适用于可变参数模板?因为参数号未定义,即使定义在主体中,它也不应该能够内联它们。 – Valkea 2011-04-05 01:13:42

我认为,在这种情况下,您可以轻松地在声明中定义的功能,并使用一些类型定义,以清除所有的语法。

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