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 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
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
> 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> ...
};
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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:
...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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>
|
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?
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |