Java-like instanceof
Oct 04, 2009
So I was hoping c++ had a Java-like instanceof function. It didnt as far as I could find... Lame. Instead I have to include an enumerated value in all of my subclasses to determine which subclass of a light i am dealing with... This sucks.

enum type {AMBIENT, DIFFUSE, PHONG};

class Light{
public:
	Light(unsigned int color, float strength, type lightType);
	unsigned int getColor();
	type getType();
	virtual void update(vector meshes);
protected:
	type lightType;
	unsigned int color;
	float strength;
};
Thoughts, anyone? Is there a better way?
Oct 04, 2009 - 7:36 pm
Aaron Sarazan
http://www.gamedev.net/community/forums/topic.asptopicid=98218&whichpage=1&512442This article has a pretty decent discussion regarding it. Our engine goes the id route that you've got, although really you don't need to include it in the constructor. Just make it a static with a virtual getter.
 
Oct 04, 2009 - 7:40 pm
Andrew
Ah, not bad. Thanks Aaron.
 
Oct 05, 2009 - 6:19 am
Paul
When you find that you need to ask what the runtime type of an object is, refactor so that it is not necessary. In this specific example, I would rather model ambient, diffuse, and phong as lighting strategies than types of lights.
 
Oct 05, 2009 - 6:30 am
Andrew
Paul, I completely forgot about the Strategy design pattern. Looks like I may need to pick up Holub again and refresh. I am going to give this more thought - Strategy should work and will be ten times better than this.

How on earth do you keep all of these design patterns fresh in your head

 
Leave a Comment
 
Links
Recent Posts Archive