我發現下面的代碼非常難以閱讀,我寫了!有沒有到如何避免在實現中重複類名和模板調用?
- 避免調用模板每個實現成員函數
- 有
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;
};
只是一個小提示:我經常發現類更容易使用的公共接口讀宣佈第一 – jeffythedragonslayer 2011-04-05 00:53:56
「美在旁觀者的眼睛」我看沒有什麼吸引力的代碼。 – 2011-04-05 01:08:18
您會發現typedefs有助於簡化代碼:例如,可以在很多地方使用'Vertex'。 –
2011-04-05 01:09:15