Suppose, I have point_c, line_c and block_c three classes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
class point_c {
public:
double x, y;
};
class line_c {
public:
vector<point_c> Pt;
void insertPoints();// insert points on line
};
class block_c {
public:
vector<point_c> Pt;
vector<line_c> Ln;
void insertLines();
void insertPoints();
};
|
As you can see, lines are composed of many points, and blocks are composed of lines and points. And I defined some member functions, so that I can add points or lines as required by line and block.
But, here comes the problem, if I use
line.insertPoints()
, or
block.insertPoints()
, the points will be stored in line or block. That is not what I want.
I want have a single place to store all points, a single place to store all lines, and a single place to store all blocks.
Then I guess I need some global variables
1 2 3
|
vector<point_c> Pt;
vector<line_c> Ln;
vector<block_c> Bk;
|
and I should change the classes into:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
class point_c {
public:
double x, y;
};
class line_c {
public:
vector<size_t> PtIndex;
void insertPoints();// insert points on line
};
class block_c {
public:
vector<size_t> PtIndex;
vector<size_t> LnIndex;
void insertLines();
void insertPoints();
};
|
in this way, the member functions will create the real entities into the global vector and set an index pointing to that entity. But this approach just make me feel it is not OOP anymore. when people using these classes, they got be careful about the global variables.
So, the question is How would you guys do this? Thanks alot!