Jump to content
  • Advertisement

Archived

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

johnnyBravo

How to make function accept any class type?

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

Say ive got two classes, eg vertex2D, vertex3D and ive got a draw function eg draw() the draw requires a class to be entered into it, eg if i... vertex2D myVertex2D; draw(myVertex2D); the problem is i want my draw to be able to accept something like these: vertex3D myVertex3D; draw(myVertex3D); aswell...... So basically my function needs to accept two class types, is there any possible way of doing this?

Share this post


Link to post
Share on other sites
Advertisement
Well, one way is to derive your vector classes from a common base class, or your vector3D from your vector2D and then accept a pointer or reference to the base class.

class vector2D {
};

class vector3D : public vector2D {
};

void draw(vector2D & arg);


Alternately, there''s always templates.

template <class T>
void draw(T arg);


And don''t forget straight up overloading.

void draw(vector2D arg);
void draw(vector3D arg);


Share this post


Link to post
Share on other sites
Overload the function e.g. two defintions


void draw(vertex2D* v);
{
//2D vertex drawing code here
}

void draw(vertex3D* v)
{
//3D vertex drawing code
}


Simple

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!