A friend convinced me to store a vector of pointers in my class:

vector<Point*> Points;

In my class instead of a vector of "real" objects (what do you call this?). The problem now is that there are several functions (out of my control ie. in a library) that accept a vector<Point>. I've been calling

vector<Point> PointersToReal(vector<Point*> &P)
{
vector<Point> Points;
for(int i = 0; i < P.size(); i++)
{
Points.push_back(*P[i]);
}
return Poitns;
}

Then calling the function, then calling

vector<Point*> PointersToReal(vector<Point> &P)
{
vector<Point*> Points;
for(int i = 0; i < P.size(); i++)
{
Points.push_back(new Point(P[i].x(), P[i].y(), P[i].z()));
}
return Poitns;
}

This just seems like a horrible horrible idea. Is there a standard solution? Or are you not supposed to store pointers if you have to pass real objects?

Thanks,

Dave

>A friend convinced me to store a vector of pointers in my class
What was your friend's reason? Why did you have to be convinced? This sounds a lot like you went against your gut feeling and now you're regretting it.

>This just seems like a horrible horrible idea.
It's generally wise to structure your data so as to avoid translations and conversions in the majority of cases. If you're constantly turning vector<Point*> into vector<Point>, you probably should have used vector<Point> as your base type to begin with. This is just common sense.

>Or are you not supposed to store pointers if you have to pass real objects?
You can store pointers if you have to pass real objects, but it gets dicey when those pointers are stored in a template container. Now you have a container type mismatch and you need ugly and inefficient workarounds to convert between the two.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.