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

发布于 2024-10-30 03:16:34 字数 1433 浏览 2 评论 0原文

我发现下面的代码非常难以阅读,所以我写了它!是否有任何方法

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

I find the code below terribly difficult to read, and I wrote it! Is there any to

  1. avoid calling template for each implemented member function
  2. avoid having ClassName::member_function_name for each implemented member function? I find Java DRYer in this regard. You don't repeat the class name everywhere.

Thanks!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

皇甫轩 2024-11-06 03:16:34

我认为在这种情况下,您可以轻松地在声明中定义函数,并使用一些 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;
};

I think that in this case, you can easily define the functions in the declaration, and use some typedefs to clear the syntax.

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;
};
长不大的小祸害 2024-11-06 03:16:34

这应该“几乎”相当于您的代码。 “几乎”,因为正如 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;
        }
};

This should be "almost" equivalent to your code. "almost", because as xDD said, the in-body definition of member function implicitly marks them as inline.

Class are private by default and Struct are public by default.

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

or with 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;
        }
};
日暮斜阳 2024-11-06 03:16:34

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

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

Being that this is a template, why not define the member functions right inside the class body?

The code needs to be available in a compilation unit for instantiation anyway, so you will not gain any compile time speedup from separating the declaration from the definition and compilers are nowadays smart enough to decide on their own whether inlining is necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文