Archived

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

New Article Complie Time Polymorphism, Help?

This topic is 5149 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

Hello- I am playing around with the new aticle, and i have some questions. am i doing something worng with this code?
#include <iostream> 
using namespace std;

template <class ShapeType>
class Shape
{
	public:
		void DrawShape(){
			reinterpret_cast<ShapeType*>(this)->Draw();
		}
		void UpdateShape(){
			reinterpret_cast<ShapeType*>(this)->Update();
		}
	protected:
		Shape(){};
};

class Rect : public Shape<Rect>
{
public:
	Rect():Shape<Rect>(){};
	void Draw(){
		cout<<"Rect Drawing..\n";
	}
	void Update(){
		cout<<"Rect Updating...\n";
	}
};

class Circle : public Shape<Circle>
{
public:
	Circle():Shape<Circle>(){};
	const void Draw(){
		cout<<"Circle Drawing..\n";
	}
	const void Update(){
		cout<<"Circle Updating...\n";
	}
};

int main()
{

	Shape<Rect>* s = new Rect;
	Shape<Circle>* r = new Circle;

	//Shape* a[2];  


	//a[0] = s;	//Can't do this!!!

	//a[1] = r;	//can't do this!!!


	s->DrawShape();
	s->UpdateShape();

	r->DrawShape();
	r->UpdateShape();

	delete r;
	delete s;


	return 0;
};

Whats the point?!! it seems like i should just make individual classes. i have to create each object as a specific type. yay. whereas in runtime polymorphism i can just create and add to a list of type base class. If don't do this:
Shape<Rect>* v = new Rect;
//and instead do this 

Shape* v  = new Rect;
the code doesnt' compile. it says it can't convert between types. am i doing something worng? [edited by - MrBeaner on November 8, 2003 5:59:16 PM]

Share this post


Link to post
Share on other sites
Hmm... a few things:

1. First, you should never ever ever create a Shape<Something>, or have a pointer to one. Shape<Something> assumes that it''s followed immediately by the rest of the Something, and if it''s wrong, bad things will happen.

2. One thing that compile-time polymorphism _CANNOT_ be used for, is a container of disparate types, such as you''re trying to do there. It is possible to combine run-time and compile-time polymorphism; see either my or aboeing''s code example in the "Discuss This Article" thread. (See aboeing''s; his naming is a lot better executed than mine.) (He''s mentioned having problems, but his code looks conceptually great, so don''t be scared away by the problems he mentioned.) But if you''re only ever doing run-time polymorphism, the compile-time side is probably overkill, so you can leave it out.

It''s important to recognize that compile-time polymorphism is not a panacea. virtual is in the language for a reason: because it provides run-time dispatch. In some cases, run-time dispatch is absolutely required, and no way of getting around it. It looks like this is one of those times.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Thanks for your help... (and in times past)

if i shouldn''t create shape with a specific type, then how can i create it without my compiler yelling at me?

Where is the "Discuss this article" thread? it doesn''t seem to exist on the boards..(or i''m stupid)

Share this post


Link to post
Share on other sites
When you view the article, there''s a "Discuss this Article" link on the table of contents on the right hand side. Check there.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites