Re: can I override private functions?
On Mar 13, 11:04 am, "Nick Keighley"
<nick_keighley_nos...@hotmail.com> wrote:
I take it this is wrong:-
class Direct_draw
{
public:
Direct_draw ();
virtual ~Direct_draw ()
{}
private:
virtual void draw_primary () = 0;
};
class Dd_animation: public Direct_draw
{
public:
Dd_animation()
{}
~Dd_animation()
{}
private:
virtual void draw_primary ()
{}
};
Direct_draw::Direct_draw ()
{
draw_primary();
}
When the Direct_draw constructor is called, the object is still a
Direct_draw object, not a Dd_animation object as you might expect.
This is why, during the Direct_draw constructor call, draw_primary()
is still a pure virtual method. Your linker error is probably
something along the lines of "attempt to call pure virtual method"?
One solution to this problem is to have a public initialize() method,
which must be called manually after any Direct_draw-derived object is
created. Any initialization you need to do, including virtual method
calls, can be included in initialize(). The problem with this is that
you might forget to call it, resulting in bugs in your code.
Another solution would be lazy initialization. Here you'd call
initialize() in each method that requires the object to be
initialized. The initialize() method would only run the initialization
code if it has not already been run. The problem with this is that you
will probably have to remember to call initialize() in each of your
derived methods. If you forget, you might run a method on an
uninitialized object, resulting in bugs.
Other people in this group will probably suggest better solutions.
Regards,
Markus.