Templates for Units conversion

This is carrying on from the question I asked in this post.

http://www.cplusplus.com/forum/general/82053/#msg440592


I don't know much about templates, so I thought I would ask if the method I outlined in the post above could be done more easily with templates.

This is my very basic understanding of templates:

1. You can have a template function, which will work for any type of object as an argument.

2. You can have a template class which provides a set of algorithms.

With 1. and 2. above, Do I have to write a function for each object type? I thought the idea of templates was to avoid this, but how does it work for Volume<Cone> and Volume<Sphere> for example?

I can understand how templates work with a collection of objects, as a collection has common methods & properties, no matter what the underlying object is. For example they have: Count, Insert, Delete, Sort, Find. If the underlying objects are numerical, they might have Sum, Mean, StdDev etc.

Also, I understand one can have overloaded operators in templates, how is this different from inheriting operators?

I am wondering if there was any benefit in using templates for Unit Conversions, and how that might work?

I am also interested in 2D & 3D objects, because they have similar operations like Move & Rotate, Area, Perimeter etc. For Area and Perimeter, the functions are exactly the same for planar objects, irrespective of whether they are 2D or 3D.

I envisage having this sort of inheritance:

1
2
3
4
5
6
7
8
class CGeometry {};
class CPoint: public CGeometry {};
class CLine:  public CGeometry {};
class CShape:  public CGeometry {};

class CRegPoly:  public CShape {}; //Regular Polygon
class CIregPoly:  public CShape {}; //Irregular Polygon
class CCircle:  public CShape {}; 


I would like to have 2D & 3D version of all these above things.

With the CPoint class I would like to have functions / operators to do Magnitude, UnitVector, dot product, cross product, triple product, add, difference, scalar product plus others. I have already written these - just would like to know how they might work with templates.

I am not asking for you guys to write all the code for me - just some pointers, examples to get me going in the right direction.

BTW, this is not an assignment or anything, - my school days were over long ago.

Thanks in advance.
Doesn't sound like a good use of templates to me, for example area you would need to either specialize the templates for each shape/type or check it's type in the function and perform the appropriate operation.
@naraku9333

Thanks for your reply.

Ok, so I might be able to have templates that work with collections of objects:

TheIdeasMan wrote:
I can understand how templates work with a collection of objects, as a collection has common methods & properties, no matter what the underlying object is. For example they have: Count, Insert, Delete, Sort, Find. If the underlying objects are numerical, they might have Sum, Mean, StdDev etc.


I will see if I can figure out how to do that. I guess my first thought would be to see if there is already a STL or Boost Collection template.

I would also like to nest these collections.

Cheers.

Topic archived. No new replies allowed.