I have a Triangle class which has members

Point P1, P2, P3;

So generally I pass the constructor 3 points.

Point A(1,1,1);
Point B(1,1,0);
Point C(0,0,1);
Triangle T(A,B,C);

Then I have a bunch of functions that I can pass point's to such as to find intersections and distances etc.

However, now I would like to make a triangle which stores pointers to points, so that when I change the positions of the points, the triangle is automatically updated.


Point A(1,1,1);
Point B(1,1,0);
Point C(0,0,1);
Triangle(&A, &B, &C);

This way when I do


The triangle now thinks the points (3,1,1), (1,1,0), (0,0,1) are its three points.

Is there some way to do this dual functionality in one class? Or do I need to make a ValueTriangle and a PointerTriangle class?



The classic solution is a union, but you can't have a union of points unless the Point class has a trivial constructor.

Another option is to store objects of a proxy class instead of storing Point objects directly. The proxy class will handle the disconnect between objects and pointers, and your Triangle class can largely ignore it.

You can also make your Triangle class a template class and then have a pointer specialization, but that's really just a cute way of having two separate classes in this case. ;)

commented: I like option #2 =) +4