Right now this is my setup:

I have these classes
Point2
Point3
Vector2
Vector3
Ray2
Ray3

Both Point classes and both Vector are independent. Ray2 contains a Point2 and a Vector2 object, and Ray3 contains a Point3 and a Vector3 object.

I have two questions.

1) Should I somehow just have a Point class that has a way to make a Point2 and a Point3 (and same for the Vector classes)

2) As I mentioned, the Ray classes just contain a Point and a Vector of the same dimension as the ray. Should this be a separate class? or again somehow derived from one of the other ones?

I have a very basic understanding of this idea and of virtual classes- if you have a class "animal" it doesn't make sense to give it a name, you first have to make a subclass "dog" before you can name it, but I've never implemented either of these ideas so I'm not sure when to apply them.

Any light you can shed will be much appreciated!

Thanks,

Dave

What's the difference between the 2 and 3 versions? How are Point2 and Point3 different, for example?

Point2 contains
double x, y;

Point3 contains
double x,y,z;

but they have different functions. Well in the case of Point they dont really have any functions at all, but with like my Line2 class, it has an IntersectLine() function that uses the y=mx+b type equations, which clearly do not apply in 3d. so my Line3 should also have an IntersectLine() function, but it will be very different.

Ed was just thinking that if the interface can be molded to fit, you can do away with the separate explicit classes and use templates:

enum {X, Y, Z};

template <int Dim>
struct Point {
  double coord[Dim];
};

sure sure, but then how does it know which functions to use? Like then I'd have to have an Intersect2 and an Intersect3 function , right? So we've just moved the problem?

> but then how does it know which functions to use?
Functions can be overloaded on a template instantiation:

T Intersect(Point<2> a);
T Intersect(Point<3> a);

Or you can do the same thing as with Point and add a template parameter for the dimensions and adjust the implementation of Intersect with template specialization since it's data driven:

template <int Dim>
class Line {
  Point<Dim> a, b;
};

template<>
class Line<2> {
public:
  void Intersect(Point<2> a);
};

template<>
class Line<3> {
public:
  void Intersect(Point<3> a);
};
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.