 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Martin Wartens Guest
|
Posted: Tue Feb 24, 2004 7:21 pm Post subject: Howto make an interface without abstract base class |
|
|
Hi,
I would like to specify an interface for some classes with out
deriving from an abstract base class. In the example below, the
declaration of the pure virtual function "draw" in the abstract base
class "AbstractShape" forces any derived class to have a function
"draw". Otherwise the compiler will complain and give me an
easy-to-understand error message.
Now I want to force a function "draw" for some classes without having
to derive from a common base class. I want the compiler to check this
and give meaningful error messages. Of course, there will be an error
if someone tries to use a "Circle" class that has no "draw", but I
want to check it before.
How can I do this? What kind of problem is this? I am a little bit
lost and don't have the right keywords for a search.
Thank you,
Martin
class AbstractShape {
public:
virtual void draw() const = 0;
};
class Circle: public AbstractShape
{
public:
virtual void draw() const;
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Teh (tî'pô) Guest
|
Posted: Wed Feb 25, 2004 3:15 pm Post subject: Re: Howto make an interface without abstract base class |
|
|
Martin Wartens proclaimed:
| Quote: | Hi,
I would like to specify an interface for some classes with out
deriving from an abstract base class. In the example below, the
declaration of the pure virtual function "draw" in the abstract base
class "AbstractShape" forces any derived class to have a function
"draw". Otherwise the compiler will complain and give me an
easy-to-understand error message.
Now I want to force a function "draw" for some classes without having
to derive from a common base class. I want the compiler to check this
and give meaningful error messages. Of course, there will be an error
if someone tries to use a "Circle" class that has no "draw", but I
want to check it before.
How can I do this? What kind of problem is this? I am a little bit
lost and don't have the right keywords for a search.
Thank you,
Martin
class AbstractShape {
public:
virtual void draw() const = 0;
};
class Circle: public AbstractShape
{
public:
virtual void draw() const;
};
|
Here's a simple way. It has the following limitations:
* Draw must be public.
* Draw can have any return value (not just void).
* Draw must not require any explicit arguments.
Each of these limitations can probably be treated but I think this
solution should be good enough for what you need.
class With {
public:
void draw();
};
class WithOut {
};
template <class T>
void ForceDraw(T& t) { t.draw(); }
// Instantiate ForceDraw for wanted classes
template void ForceDraw<With>(With&); // OK
template void ForceDraw<WithOut>(WithOut&); // ERRROR
The Error message CC gives is (IMO) easy-to-understand:
Error: draw is not a member of WithOut.
Where: While instantiating "ForceDraw<WithOut>(WithOut&)".
Where: Instantiated from non-template code.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Wed Feb 25, 2004 11:53 pm Post subject: Re: Howto make an interface without abstract base class |
|
|
Martin Wartens wrote:
| Quote: | Hi,
I would like to specify an interface for some classes with out
deriving from an abstract base class. In the example below, the
declaration of the pure virtual function "draw" in the abstract base
class "AbstractShape" forces any derived class to have a function
"draw". Otherwise the compiler will complain and give me an
easy-to-understand error message.
Now I want to force a function "draw" for some classes without having
to derive from a common base class. I want the compiler to check this
and give meaningful error messages. Of course, there will be an error
if someone tries to use a "Circle" class that has no "draw", but I
want to check it before.
How can I do this? What kind of problem is this? I am a little bit
lost and don't have the right keywords for a search.
snip |
I think the only way to do this is in C++ is to use function
templates, e.g.
template<class Shape>
void drawTwice(Shape & shape)
{
shape.draw();
shape.draw();
}
I wonder whether this is really going to solve your problem though.
If you want to have collections of arbitrary shapes you are still
going to have to derive them from a common base class. Why do you not
want to do this?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Andre Heinen Guest
|
Posted: Wed Feb 25, 2004 11:54 pm Post subject: Re: Howto make an interface without abstract base class |
|
|
On 24 Feb 2004 14:21:01 -0500, [email]martin.wartens (AT) my-mail (DOT) ch[/email] (Martin
Wartens) wrote:
| Quote: | Hi,
I would like to specify an interface for some classes with out
deriving from an abstract base class.
|
Why?
| Quote: | snip
Of course, there will be an error
if someone tries to use a "Circle" class that has no "draw", but I
want to check it before.
|
I don't understand. If a Circle has no draw(), doesn't it mean
that after all you haven't forced your classes to have said
function?
--
Andre Heinen
My address is "a dot heinen at europeanlink dot com"
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gerhard Menzl Guest
|
Posted: Wed Feb 25, 2004 11:59 pm Post subject: Re: Howto make an interface without abstract base class |
|
|
Martin Wartens wrote:
| Quote: | I would like to specify an interface for some classes with out
deriving from an abstract base class. In the example below, the
declaration of the pure virtual function "draw" in the abstract base
class "AbstractShape" forces any derived class to have a function
"draw". Otherwise the compiler will complain and give me an
easy-to-understand error message.
Now I want to force a function "draw" for some classes without having
to derive from a common base class. I want the compiler to check this
and give meaningful error messages. Of course, there will be an error
if someone tries to use a "Circle" class that has no "draw", but I
want to check it before.
How can I do this? What kind of problem is this? I am a little bit
lost and don't have the right keywords for a search.
|
I suggest you try the keywords "template", "generic", and "compile-time
polymorphism".
| Quote: | class AbstractShape {
public:
virtual void draw() const = 0;
};
class Circle: public AbstractShape
{
public:
virtual void draw() const;
};
|
class Circle
{
public:
void draw const ();
};
template <typename Shape> void generic_draw (Shape const& shape)
{
shape.draw ();
}
The usage is:
int main ()
{
Circle c;
generic_draw (c);
}
Avoiding excessive coupling through inheritance is one big advantage of
templates. For a class to satisfy the interface defined by the function
template generic_draw<>, it suffices that there exists a member function
draw that has the correct signature. It may be virtual, non-virtual, or
static. You can even pass a reference to an abstract base class that has
a pure virtual function called draw, thus combining both kinds of
polymorphism available in C++. Other than that, the classes need not be
related at all.
Is that what you were looking for?
Gerhard Menzl
--
Humans may reply by replacing the obviously faked part of my e-mail
address with "kapsch".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|