inheritance - Specialising on two ancestor class in C++ -
i have defined abstract interface consisting of 3 classes:
class rendering_context {...} class window : public rendering_context {...} class offscreen : public rendering_context {...}
this allows various client objects ignore type of rendering context they’re in. far good.
i defined concrete implementations:
class sdl::window : public window {...} class sdl::offscreen : public offscreen {...}
this works expected, , client classes happy no idea whether on-screen / off-screen / sdl-based or not.
my confusion:
i have sdl-specific functionality applies in rendering contexts, i.e. not in 1 or other of sdl::window
or sdl::offscreen
.
this suggests class of sdl::rendering_context
needs pulled out somewhere, how class hierarchy?
if make concrete classes so:
class sdl::window : public window, public sdl::rendering_context {...} class sdl::offscreen : public offscreen, public sdl::rendering_context {...}
and assuming sdl::rendering_context
inherits rendering_context
haven’t inherited rendering_context
twice implicitly? fact rendering_context
, in case, abstract base class, no members , several pure virtual functions, make okay?
is same diamond problem? seems might be, difference there’s no ambiguity base class method call: pure virtual , concrete implementation @ bottom of hierarchy executed in practice.
[n.b. none of class declarations above, illustrate hierarchy. current code compiles , works, duplicates functionality.]
and assuming sdl::rendering_context inherits rendering_context haven’t inherited rendering_context twice implicitly?
if problem, consider virtual inheritance, in rendering_context
shared base class. if wish this:
class window : public virtual rendering_context {...} class offscreen : public virtual rendering_context {...}
Comments
Post a Comment