Archived

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

johnnyBravo

How to make function accept any class type?

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
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