A
Andrew
Hi,
I need some advice regarding a C++ implementation.
Suppose we have 3 interfaces, a base interface and two extending it:
struct IInterface { virtual void m() = 0; };
struct IInterface2 : virtual IInterface { virtual void m2() = 0; };
struct IInterface3 : virtual IInterface { virtual void m3() = 0; };
The interfaces are implemented like this:
We implement the common base interface into an abstract class:
struct BaseAbstract : virtual IInterface { virtual void m(); };
and then implement the other 2 interfaces as 2 concrete classes:
struct Concrete2 : BaseAbstract, IInterface2 { virtual void m2(); };
struct Concrete3 : BaseAbstract, IInterface3 { virtual void m3(); };
Now, this will give C4250 saying that the concrete classes inherits
the
method m() implemented by the BaseAbstract class via dominance. This
can be
overlooked but I was wondering if the design is OK.
One other way is both Concrete classes implement for their own the
IInterface methods and BaseAbstract won't be needed but that it's not
OK I
guess since it's duplicate code.
Another way would be that IInterface2 and IInterface3 be separate
interfaces in the way that they do not inherit from the base
IInterface. But
the reason they do inherit is that I want to be able to use
IInterface2 and
IInterface3 as an IInterface too or else said, given an object
pointing to
IInterface2 i want to be able to use IInterface methods.
COM for example uses the QueryInteface method to obtain the other
interfaces
of an object but I do not want to use COM for this.
The client knows which type of object does it have given an
IInterface*, by
using an get_type() method from IInterface. (i did not include it in
order
to look simpler). It will then cast to the appropiate interface
(another
option would be using the dynamic_cast with RTTI on but i thought it's
simpler with get_type() ).
I need some advice regarding a C++ implementation.
Suppose we have 3 interfaces, a base interface and two extending it:
struct IInterface { virtual void m() = 0; };
struct IInterface2 : virtual IInterface { virtual void m2() = 0; };
struct IInterface3 : virtual IInterface { virtual void m3() = 0; };
The interfaces are implemented like this:
We implement the common base interface into an abstract class:
struct BaseAbstract : virtual IInterface { virtual void m(); };
and then implement the other 2 interfaces as 2 concrete classes:
struct Concrete2 : BaseAbstract, IInterface2 { virtual void m2(); };
struct Concrete3 : BaseAbstract, IInterface3 { virtual void m3(); };
Now, this will give C4250 saying that the concrete classes inherits
the
method m() implemented by the BaseAbstract class via dominance. This
can be
overlooked but I was wondering if the design is OK.
One other way is both Concrete classes implement for their own the
IInterface methods and BaseAbstract won't be needed but that it's not
OK I
guess since it's duplicate code.
Another way would be that IInterface2 and IInterface3 be separate
interfaces in the way that they do not inherit from the base
IInterface. But
the reason they do inherit is that I want to be able to use
IInterface2 and
IInterface3 as an IInterface too or else said, given an object
pointing to
IInterface2 i want to be able to use IInterface methods.
COM for example uses the QueryInteface method to obtain the other
interfaces
of an object but I do not want to use COM for this.
The client knows which type of object does it have given an
IInterface*, by
using an get_type() method from IInterface. (i did not include it in
order
to look simpler). It will then cast to the appropiate interface
(another
option would be using the dynamic_cast with RTTI on but i thought it's
simpler with get_type() ).