|
-
October 20th, 2009, 01:40 AM
#5
Re: Design Problem - Whether to use Templates, Inheritance, or Nothing
to start of with the widgets. The problem is the WIDGET_TEST parameters.
One solution is to use a base class for these parameters and then introduce an intermediary base class that does the casting for you.
Here is an example which does not do safe casts etc.. but get the idea across
Code:
class WidgetTest
{
public:
virtual ~WidgetTest() = 0;
};
WidgetTest::~WidgetTest(){}
class WidgetInterface
{
public:
virtual ~WidgetInterface(){}
virtual bool verify(const WidgetTest& wt) const = 0;
virtual float getAccuracy(const WidgetTest& wt) const = 0;
};
template<typename WT>
class WidgetTestCast : public WidgetInterface
{
public:
virtual bool verify(const WidgetTest& wt) const
{
return verifyIMPL(static_cast<const WT&>(wt));
}
virtual float getAccuracy(const WidgetTest& wt) const
{
return getAccuracyIMPL(static_cast<const WT&>(wt));
}
private:
virtual bool verifyIMPL(const WT& wt) const = 0;
virtual float getAccuracyIMPL(const WT& wt) const = 0;
};
class FooWidgetTest : public WidgetTest
{
public:
FooWidgetTest():
blah(1)
{
}
virtual ~FooWidgetTest(){}
int blah;
};
class FooWidget : public WidgetTestCast<FooWidgetTest>
{
private:
virtual bool verifyIMPL(const FooWidgetTest& wt) const
{
std::cout << "verify " << wt.blah << '\n';
return true;
}
virtual float getAccuracyIMPL(const FooWidgetTest& wt) const
{
std::cout << "getAccuracy " << wt.blah << '\n';
return 1.0;
}
};
Now you have a common base class for the widgets to store in a container, but don't have to explicitly cast in the subclasses
Last edited by souldog; October 20th, 2009 at 01:46 AM.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|