"James Wholar" <_@_.a> wrote
[8<8<8<]
> Men.. hvordan kan dette gøres?
Du kan måske bruge design patternet "Template Method" (om ikke har noget med
C++ templates at gøre).
Det findes beskrevet i bogen:
Design Patterns: Elements of Reusable Object-Oriented Software
Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
ISBN 0-201-63361-2
Det vil kunne se ca. sådan ud:
<code>
#include <iostream>
using std::cout; using std::endl;
class scene
{
public:
scene();
virtual ~scene();
void render(); // not virtual
protected:
virtual void pre_render();
virtual void do_render() = 0;
virtual void post_render();
private:
// copy constructor and copy assignment
// delcared but not implemented, to prevent
// accidental usage
scene(const scene&);
scene& operator=(const scene&);
};
class billboard : public scene
{
protected:
virtual void pre_render();
virtual void do_render();
virtual void post_render();
};
class table : public billboard
{
protected:
virtual void do_render();
};
inline scene::scene()
{
}
inline scene:
![Meget forvirret](images/smileys/confused.gif)
scene()
{
}
void scene::render()
{
pre_render();
do_render();
post_render();
}
void scene::pre_render()
{
cout << "scene::pre_render" << endl;
}
void scene::post_render()
{
cout << "scene::post_render" << endl;
}
void billboard::pre_render()
{
cout << "billboard::pre_render" << endl;
cout << "setup matrix" << endl;
}
void billboard::do_render()
{
cout << "billboard::do_render" << endl;
}
void billboard::post_render()
{
cout << "billboard::post_render" << endl;
cout << "cleanup matrix" << endl;
}
void table::do_render()
{
cout << "table::do_render" << endl;
}
void test_render(scene& scene_arg)
{
scene_arg.render();
}
int main()
{
table t;
test_render(t);
}
</code>
Venlig hilsen
Mogens Hansen