template of class function?

G

Gernot Frisch

Hi,


I have a class that has a funtion "DrawMe".
Now, I want to make this function work on 2 different argument types
that provide all the same methods:

class ASCIIDraw
{
MoveTo();
DrawTo();
};

class GDIDraw
{
MoveTo();
DrawTo();
};

class MyDraw
{
template<class T>DoDraw(T& DrawObject);
};

(How) can I do this?


--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
M

Moonlit

Hi,

Gernot Frisch said:
Hi,


I have a class that has a funtion "DrawMe".
Now, I want to make this function work on 2 different argument types that
provide all the same methods:

class ASCIIDraw
{
MoveTo();
DrawTo();
};

class GDIDraw
{
MoveTo();
DrawTo();
};

class MyDraw
{
template<class T>DoDraw(T& DrawObject);
};

(How) can I do this?

As far as I can see your code already does that. Do you mean this?
//---------------------------------------Code----------------------------
#include <stdlib.h>
#include <memory.h>
#include <iostream>
using namespace std;

class ASCIIDraw
{
public:
void MoveTo(){};
void DrawTo(){ cout << "Hello" << endl; };
};

class GDIDraw
{
public:
void MoveTo(){};
void DrawTo(){};
};

class MyDraw
{
public:
template<class T>void DoDraw(T& DrawObject)
{
DrawObject.MoveTo();
DrawObject.DrawTo();

}
};



int main()
{

MyDraw Draw;

ASCIIDraw ASCII;
Draw.DoDraw( ASCII );

return 0;
}
//------------------------------------------------End
Code--------------------------------
 
M

Moonlit

Hi,

Forgot to say that if you can come up with a generic interface for bot
classes you might create a generic Draw class (with MoveTo and DrawTo as
(pure) virtual functions and derive the ASCII and GDIDraw classes from them.
Move the DoDraw class to the base class and just call DoDraw on the object.

Regards, Ron AF Greve.
 
G

Gernot Frisch

Forgot to say that if you can come up with a generic interface for
bot classes you might create a generic Draw class (with MoveTo and
DrawTo as (pure) virtual functions and derive the ASCII and GDIDraw
classes from them. Move the DoDraw class to the base class and just
call DoDraw on the object.

So, I get:

class base
{
virtual void MoveTo(int, int)=0;
void DrawChild(const base& d2);
};

class Draw2:public base
{
void MoveTo(int x, int y);
void DrawChild(const Draw2& d2); // or const base& ...
reinterpret_cast<d2> ...
};
 
J

JKop

Gernot Frisch posted:
Hi,


I have a class that has a funtion "DrawMe".
Now, I want to make this function work on 2 different argument types
that provide all the same methods:

class ASCIIDraw
{
MoveTo();
DrawTo();
};

class GDIDraw
{
MoveTo();
DrawTo();
};

class MyDraw
{
template<class T>DoDraw(T& DrawObject);
};

(How) can I do this?

Geez, get some inheritence in there:

class DrawObject
{
public:

virtual void MoveTo() = 0;
virtual void DrawTo() = 0;
};

class ASCIIDraw : public DrawObject
{
public:

virtual void MoveTo()
{
//code goes here
}

virtual void DrawTo()
{
//code goes here
}
};

class GDIDraw : public DrawObject
{
public:

virtual void MoveTo()
{
//code goes here
}

virtual void DrawTo()
{
//code goes here
}
};


void SomeFunc(DrawObject& draw_object)
{
draw_object.DrawTo();
}

int main()
{
ASCIIDraw a;
GDIDraw b;

SomeFunc(a);
SomeFunc(b);
}


Hope that helps,

-JKop
 
M

Moonlit

Hi,

Gernot Frisch said:
So, I get:

class base
{
virtual void MoveTo(int, int)=0;
void DrawChild(const base& d2);
};

class Draw2:public base
{
void MoveTo(int x, int y);
void DrawChild(const Draw2& d2); // or const base& ...
reinterpret_cast<d2> ...
};

Close but not good enough ;-) , but I see JKop already gave the answer.Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

template trouble 0
int(a) vs (int)a 19
explicit specialization od c'tor 11
cast operator for built in to class? 5
reference to array? 13
pointer to function 4
date comparison 2
queue where I can delete in the middle? 3

Members online

Forum statistics

Threads
474,175
Messages
2,570,947
Members
47,498
Latest member
yelene6679

Latest Threads

Top