如何避免在实现中重复类名和模板调用
How to avoid repeating class name and template call in implementation?
我发现下面的代码非常难以阅读,我写了它!有没有
- 避免为每个实现的成员函数调用模板
- 避免为每个实现的成员函数设置
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;
};
我认为在这种情况下
,您可以轻松地在声明中定义函数,并使用一些typedefs来清除语法。
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;
};
这应该"几乎"等同于您的代码。"几乎",因为正如 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;
}
};
既然
这是一个模板,为什么不在类体内定义成员函数呢?
无论如何,代码都需要在编译单元中可用以进行实例化,因此您将不会从将声明与定义分离中获得任何编译时间加速,并且编译器现在足够聪明,可以自行决定是否需要内联。
相关文章:
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 避免在人为的重载函数调用中拼写出类型
- 使用 pybind11 调用 Python 函数时避免复制输入数据
- 如何让编译器知道要调用函数的哪个重载以避免歧义?
- 在解压缩可变参数模板时避免"recursive"函数调用,直到运行时条件
- 使用 emplace_back 避免移动构造函数调用的最佳方法?
- 应由于计算费用而避免对STD的多个调用::发现,否则这是可以接受的情况
- 优先考虑GRPC调用以避免服务器过载
- 如何避免参数数量依赖于条件的调用分支?
- 在这种情况下是否可以避免使用虚拟方法调用?
- 避免在 ctor 初始值设定项列表中的字符串赋值中使用双重调用函数
- 当被调用方法使用调用方已锁定的同一锁时,如何避免死锁
- 如何避免在Qt项目中调用Python脚本时出现分段错误
- 调用委托构造函数时避免复制
- 在使用Embind的Javascript中,仅仅调用deleteLater()就足以避免内存泄漏吗
- C++多线程在函数调用期间避免交错
- 在函数调用时避免模板规范
- 如何构建JNI调用以避免内存泄漏