I am creating a CAD software in which I have a class point & a class line.
#include <vector>
using namespace std;
class point
{
private:
vector<int>* child;
int id;//this is unique id of a point
double x;//absolute x, y, z co-ord
double y;//
double z;//
};
class line
{
private:
point *a;
point *b;
int id;//this is unique for each line object
}
any line object is the child of 2 point objects.
so if i delete a point of a line then the line object shud also be deleted.
For this i want to store the id of all children (line, circle, triangle,….) of a point object in a vector child (instance variable of class point as shown in code).
my question is is my approach correct?
i mean that, there will be around 100’s of point objects in an execution.
since each vector allocates some extra memory, there will be a lot of allocated memory which will not be used in an execution.
can anybody suggest an alternative way of storing unknown no of int as a sequence in each point object?
You have already done this correctly. I’d suggest that you dont’t use a pointer to your
vectorbut simply avectoritself:This makes handling it much easier, and you don’t have to allocate/deallocete it by yourself.
If you really need to have a dynamically allocated pointer, you have to call
newanddelete. For this to behave correctly, you have to define both a constructor and a destructor in yourpointclass:But I still recommend that you don’t use a pointer to a
vectoras I have explained above.