2011-04-05 14 views
5

我發現下面的代碼非常難以閱讀,我寫了!有沒有到如何避免在實現中重複類名和模板調用?

  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

回答

1

因爲這是一個模板,爲什麼不在類體內定義成員函數呢?

代碼需要在編譯單元中用於實例化,所以你不會獲得任何編譯時加速從分離聲明和定義,編譯器現在足夠聰明以決定是否需要內聯。

1

這應該是「幾乎」相當於您的代碼。 「差不多」,因爲正如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

1

我認爲,在這種情況下,您可以輕鬆地在聲明中定義的功能,並使用一些類型定義,以清除所有的語法。

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