如何避免在实现中重复类名和模板调用?
问题描述:
我发现下面的代码非常难以阅读,我写了!有没有到如何避免在实现中重复类名和模板调用?
- 避免调用模板每个实现成员函数
- 有
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所说,成员函数的in-body定义将其隐含地标记为内联。
默认情况下,这个类是私有的,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;
}
};
答
我认为,在这种情况下,您可以轻松地在声明中定义的功能,并使用一些类型定义,以清除所有的语法。
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;
};
只是一个小提示:我经常发现类更容易使用的公共接口读宣布第一 – jeffythedragonslayer 2011-04-05 00:53:56
“美在旁观者的眼睛”我看没有什么吸引力的代码。 – 2011-04-05 01:08:18
您会发现typedefs有助于简化代码:例如,可以在很多地方使用'Vertex'。 –
2011-04-05 01:09:15