473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

template of class function?

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
Jul 22 '05 #1
5 1297
Hi,

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2q************@uni-berlin.de...
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--------------------------------
--
-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

Jul 22 '05 #2
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.
"Moonlit" <news moonlit xs4all nl> wrote in message
news:41***********************@news.xs4all.nl...
Hi,

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2q************@uni-berlin.de...
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--------------------------------
--
-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


Jul 22 '05 #3
> 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> ...
};
Jul 22 '05 #4
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
Jul 22 '05 #5
Hi,

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2q************@uni-berlin.de...
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> ...
};


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

Regards
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
7
by: quarup | last post by:
I want to specialize a template function that lives inside a class, but am getting a compile error in VS.net 2003. Here's my code: template <class T> class A { public: template <class U> void...
12
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
8
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
2
by: Alan | last post by:
Does a template class declaration, like template <class T> have to come immediately prior to the declaration of the function, e.g., T do_something (T something) { . . . } that uses it?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.