Archived

This topic is now archived and is closed to further replies.

Playing with templates

This topic is 5148 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying out templates in my latest project. I wrote a simple Point class. I'm sure there are some problems with it, so I thought I'd post it here for a critique.
/** Generic point class. */
template
class Point
{
	Type v_[Dim];
public:
	const Type &operator[](int j) {return v_[j];}
};

/** Specialization of Point with 2 dimensions. */
template
class Point
{
public:
	union{Type x_, u_;};
	union{Type y_, v_;};
        
	Point(const Type &x, const Type &y)
		: x_(x), y_(y) {};

	const Type &operator[](int j)
	{
		switch(j)
		{
			case 0: return x_; break;
			case 1: return y_; break;
		}
		return v_[j];
	}
};
  
What I originally wanted to do in the specialization was make x_ and y_ const references that point to v_[0] and v_[1]. But unfortunately that doesn't work. As a side question, what do you think of making namespaces an integral part of classes differentiation? Is setting up classes like video::System, audio::System, and input::System a bad thing? Questions, comments, critiques? [edited by - dcosborn on November 13, 2003 1:40:57 AM]

Share this post


Link to post
Share on other sites
In my opinion, you are making things overly complicated for just a simple point class. What''s the point of templating it? Just have a Point2D and Point3D class instead, with public x and y members (and z for 3D points). Why make the members public I hear you say? Well, in a point class there is no advantage to hiding your data members, it will just overcomplicate your code once again.

Also, I would go against enclosing each major class in its own namespace, IMO I would just use a namespace with a large library or module.

Share this post


Link to post
Share on other sites
I agree with you on most of your points.

quote:
Why make the members public I hear you say?


In my class the members are public. The only reason v_ is private is because it is accessed through operator[] by point[0] instead of point.v_[0].

quote:
In my opinion, you are making things overly complicated for just a simple point class.


Templates might be overkill, but a templated point class is more generic and only requires you to write one implementation (or maybe 3 if you want to specialize it for 2 and 3 dimensions like I did).

quote:
Also, I would go against enclosing each major class in its own namespace, IMO I would just use a namespace with a large library or module.


I have been using namespaces for large modules (like "env" for platform independent abstractions and "video" for video interfaces), but I still want to know whether its a bad practice to make classes or types with the same names in different namespaces.

Thanks for your comments though, glad to see you agree with me.

Share this post


Link to post
Share on other sites
quote:
Original post by dcosborn
In my class the members are public. The only reason v_ is private is because it is accessed through operator[] by point[0] instead of point.v_[0].


And the reason you want to do that is because you want to provide bounds checking? That''s about the only reason I can see. Bounds checking at debug mode, and non-bounds checking at release mode. However, you should remember that Point is a very trival and perhaps even heavily used class that any additional checks would slow down your application. Bearing that in mind, most people would not even do bounds-checking for their Point class. Or even if they do, would actually use the non-bounds-checking version later because of performance issue and the pain of switching between versions. And so, bearing that later point in mind(pun intended), a trival accessor in the case of operator [] is actually pointless(pun intended again). Not to mention that point.x is actually more natural than point[0]. The first would be ''parsed'' by your brain immediately, while the latter might actually requires a single tick of your brain. Make your codes as clear as possible.

quote:

Templates might be overkill, but a templated point class is more generic and only requires you to write one implementation (or maybe 3 if you want to specialize it for 2 and 3 dimensions like I did).


So... why not provide a Point2D class and a Point3D class? At the most you need is a Point4D for mathematical operations with your 4x4 matrix if you ever decide to. And when that comes, if you did it the old way, chances are you will specialize it as well again. So you get 1 base template, and 3 specializations. Why not just go with 3 templates?

quote:

I have been using namespaces for large modules (like "env" for platform independent abstractions and "video" for video interfaces), but I still want to know whether its a bad practice to make classes or types with the same names in different namespaces.


That is a pretty gray area. Namespaces is useful for code organization, perhaps even versioning. However, nested namespace gets ugly after a while, if you are those who fully qualifies your variables. If you do not fully qualifies them, and use the ''using namespace'' declarations, then ask yourself, why put them into namespaces in the first place?

Share this post


Link to post
Share on other sites
quote:
And the reason you want to do that is because you want to provide bounds checking?


No the reason I want to do that is because it seems more natural to access a n dimensional point variable as an array rather than knowing anything about a v_ member variable.

quote:
And when that comes, if you did it the old way, chances are you will specialize it as well again.


I think I could only specialize for 2, 3, and maybe 4 dimensions because the higher dimensions don''t have special names for their components (that I know of).

quote:
why not provide a Point2D class and a Point3D class?


Pretty much the reason I wrote the class this way was so that I could access the components in 2 and 3 dimensional versions in a natural way (using .x, .y, etc) while still providing support for n dimensional versions at the same time. I think that writing separate classes for each dimension isn''t as flexible. Also, why hard code something like the dimension into the variable name when you could do it all generically by using a few extra language features. Plus, by doing it this way I can easily use the class in other template functions I might define in the future.

Share this post


Link to post
Share on other sites