Sign in to follow this  

type checking

This topic is 3662 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'm trying to code a function that can take any 'shape' and draw it to screen, but I don't know how to do it :
struct Shape {};
struct Circle : public Shape
	{
	int x,y,radius;
	};
struct Rectangle : public Shape
	{
	int x,y,w,h;
	};
// [...]

void Renderer::Draw(Shape& S)
	{
	//if S is base class do nothing
	//if S is Circle draw it using its x,y,radius
	//same for rectangles or anything
	}

Share this post


Link to post
Share on other sites
The problem is don't want to delegate the drawing to the shapes (maybe I'll have no choice ?),
I tired overloading :

Renderer::Draw(Shape*);
Renderer::Draw(Circle*);
Renderer::Draw(Rectangle*);

I've got a Object that has a Shape* member :

S_BounceAroundScreen bouncy;
Circle* C=new Circle(10);
Object ball(C,&bouncy);

if I do theRenderer.Draw(ball.myShape) well it calls the 'Shape' version although ball.myShape points to a Circle

Share this post


Link to post
Share on other sites
Given that your code looks a lot like C++, you can use dynamic_cast and typeid to get at the runtime type of an object. But, really, this is the kind of thing virtual functions are for.

Share this post


Link to post
Share on other sites

This topic is 3662 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this