Suppose I have the following hierarchy of classes:
class Entity{
public:
Entity(int idValue){id=idV alue};
private:
int id;
}
class Vertex: public Entity
{
public:
Vertex(): Entity(0){};
// More stuffs
}
class Edge: public Entity
{
public:
Edge(): Entity(1){};
// More stuffs
}
class Face: public Entity
{
public:
Face(): Entity(2){};
// More stuffs
}
The aim is to implement an object counter for classes Vertex, Edge and Face. This is very import in the context of my project. I figured out the following redesign:
class Entity{
public:
Entity(int idValue){id=idV alue; objectCounter[id]++};
~Entity(){objec tCounter[id]--};
int howManyObjects( ){
return objectCounter[id];
}
private:
int id;
static int objectCounter[3];
}
int Entity::objectC ounter[3] = {0,0,0};
Before my questions, a certain improvement in the code style would be to create enum {VERTEX,EDGE,FA CE} and substitute the magic numbers 0, 1 and 2.
In the article I considered the proposed approach too much complicated by using templates. Supposing that the counters are only for objects of these classes (or a fixed number of classes that I known a priori), do you have any criticism about my approach, possible improvements and mostly important, potential pitfalls?
Your help will be much appreciated.
class Entity{
public:
Entity(int idValue){id=idV alue};
private:
int id;
}
class Vertex: public Entity
{
public:
Vertex(): Entity(0){};
// More stuffs
}
class Edge: public Entity
{
public:
Edge(): Entity(1){};
// More stuffs
}
class Face: public Entity
{
public:
Face(): Entity(2){};
// More stuffs
}
The aim is to implement an object counter for classes Vertex, Edge and Face. This is very import in the context of my project. I figured out the following redesign:
class Entity{
public:
Entity(int idValue){id=idV alue; objectCounter[id]++};
~Entity(){objec tCounter[id]--};
int howManyObjects( ){
return objectCounter[id];
}
private:
int id;
static int objectCounter[3];
}
int Entity::objectC ounter[3] = {0,0,0};
Before my questions, a certain improvement in the code style would be to create enum {VERTEX,EDGE,FA CE} and substitute the magic numbers 0, 1 and 2.
In the article I considered the proposed approach too much complicated by using templates. Supposing that the counters are only for objects of these classes (or a fixed number of classes that I known a priori), do you have any criticism about my approach, possible improvements and mostly important, potential pitfalls?
Your help will be much appreciated.
Comment