簡體   English   中英

如何避免在實現中重復類名和模板調用?

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

我發現下面的代碼非常難以閱讀,我寫了它! 有沒有

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

由於這是一個模板,為什么不在類體內定義成員函數?

無論如何,代碼都需要在編譯單元中用於實例化,因此您不會通過將聲明與定義分開來獲得任何編譯時加速,而編譯器現在足夠聰明,可以自行決定是否需要內聯。

這應該“幾乎”等同於您的代碼。 “幾乎”,因為正如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;
        }
};

我認為在這種情況下,您可以輕松地在聲明中定義函數,並使用一些typedef來清除語法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM