如何避免在实现中重复类名和模板调用?
我发现下面的代码非常难以阅读,所以我写了它!是否有任何方法
- 可以避免为每个实现的成员函数调用模板,
- 避免为每个实现的成员函数使用
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
- avoid calling template for each implemented member function
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为在这种情况下,您可以轻松地在声明中定义函数,并使用一些 typedef 来清除语法。
I think that in this case, you can easily define the functions in the declaration, and use some typedefs to clear the syntax.
这应该“几乎”相当于您的代码。 “几乎”,因为正如 xDD 所说,成员函数的体内定义隐式地将它们标记为内联。
类默认是私有的,结构默认是公共的。
或使用 typedef :
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.
or with typedef :
既然这是一个模板,为什么不在类体内定义成员函数呢?
无论如何,代码需要在编译单元中可用以进行实例化,因此您不会因将声明与定义分离而获得任何编译时间加速,并且编译器现在足够聪明,可以自行决定是否需要内联。
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.