我发现下面的代码非常难以阅读,我写了它!
有没有?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;
};
答案 0 :(得分:1)
由于这是一个模板,为什么不在类体内定义成员函数?
代码需要在编译单元中可用于实例化,因此您不会通过将声明与定义分离来获得任何编译时加速,编译器现在足够聪明,可以自行决定是否需要内联。 / p>
答案 1 :(得分:1)
这应该“几乎”等同于您的代码。 “几乎”,因为正如xDD所说,成员函数的体内定义隐含地将它们标记为内联。
默认情况下,Class是私有的,默认情况下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;
}
};
答案 2 :(得分:1)
我认为在这种情况下,您可以轻松地在声明中定义函数,并使用一些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;
};