Cool_Coder
Cool_Coder

Reputation: 5083

creating pointer to a vector, in a class

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?

Upvotes: 0

Views: 874

Answers (3)

Salvatore Previti
Salvatore Previti

Reputation: 9080

I would solve the problem in a totally different way.

I would keep two global hashtables by id: one for points and one for lines. Then i would use a doubly linked list data structure for each point so that you know how many lines are using a given point.

I would make the access to these data structure easy through simple operations in a global container class.

I'll try to write some "pseudo" code to make it clearer (editing soon).

Upvotes: 0

Constantinius
Constantinius

Reputation: 35089

my question is how do i create this vector & where (so that it doesnt go out of scope)?

You have already done this correctly. I'd suggest that you dont't use a pointer to your vector but simply a vector itself:

private:
    vector<int> child;

This makes handling it much easier, and you don't have to allocate/deallocete it by yourself.


how do i make my child point to the created vector without memory leak?

If you really need to have a dynamically allocated pointer, you have to call new and delete. For this to behave correctly, you have to define both a constructor and a destructor in your point class:

class point
{
private:
    point() : child(new vector<int>) {}
    ~point() { delete child; }

    vector<int>* child;
    int id;//this is unique id of a point
    double x;//absolute x, y, z co-ord
    double y;//
    double z;//
};

But I still recommend that you don't use a pointer to a vector as I have explained above.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258648

You don't need to use a pointer to a vector, an object would do:

#include <vector>
class point
{
private:
   std::vector<int> child;
   int id;//this is unique id of a point
   double x;//absolute x, y, z co-ord
   double y;//
   double z;//
};

Don't forget to include <vector> and qualify your use with std::vector.

Upvotes: 2

Related Questions