More enlightenment, please.

I'm wondering if I'm breaking any C++ rules or best practices with this small bit of code, which is something I used quite a lot back in my ANSI C programming days:

1
2
3
// Ensure we don't add duplicate vertices to the list. If it is found, return a pointer to the vertex.
if(( vertex_ptr[i] = g.find_vertex( static_vertex )) != NULL)
    vertex_ptr[i] = g.add_vertex( static_vertex );  // Insert vertex, return index 


It's returning a pointer to the vertex. Now, from my old days of ansi c, this pointer could be used to modify the contents, clearly something that violates encapsulation. I did declare the function members as const:

1
2
3
4
5
6
7
8
9
10
11
12
13

class Geometry
{
private:
	vector<Point> vertex_list;
	list<Triangle> triangle_list;
protected:
public:
	Geometry();

	Point *find_vertex( const Point &p ) const;
	Point *add_vertex( const Point &p ) const;
};


Can this pointer still be used to modify the contents it points to?
Last edited on
closed account (o1vk4iN6)
Yes it can still be modified, if you don't want it to be than return a const pointer, the const on the function simply means that nothing in the class is being modified by that function. You should also be careful, if you are return a pointer to an element in a list or vector it is possible that if you add an element and the vector needs to be expanded than any previous pointers returned by the function will be invalid.

If you really don't want the data to be modified you can return a copy of the data instead, since the a "const Point*" can be reinterpreted as a non-const and than modified.
Last edited on
Copies won't do since each triangle is going to reference three vertices and also each triangle it shares an edge with. That will use a lot of memory. I wanted to use pointers as references to minimize the memory requirements.

Maybe I'll use an integer index instead, but that will still require updating to the vertices the triangles reference.

I could let each triangle hold it's own vertex, but my understanding of fp:

double x = 1.0;
double y = 1.0

x may not equal y due to rounding.

I am hoping to avoid rolling up a specialized container.

Oh, I just thought of something:

If I do:

vector<Point *> vertex_list;
list<Triangle *> triangle_list;

And allocate a memory block for each object (making sure to free/delete them when done), will that keep address returned by lists and vectors the same? It should, because it's returning a pointer to an object, not an object or reference to an object.

Last edited on
Getting this error:
1>c:\users\roberts\documents\visual studio 2010\projects\fisix\fisix\vertex.h(40): error C2803: 'operator -' must have at least one formal parameter of class type

On this line:

 
Vector operator-(const Point *p0, const Point *p1) { Vector v; v.X(p1->x - p0->x); v.Y(p1->y - p0->y); v.Z(p1->z - p0->z);  return v; }


Is this telling me one of the params needs to a static or reference to a static?

Static is in: struct Point p, as opposed to struct Point *p;
Last edited on
Topic archived. No new replies allowed.