COM: Chicken Interface

Started by
10 comments, last by HaywireGuy 19 years, 11 months ago
Nobody likes lengthy posts eh? Well I'll try to make this one short. So I have a base class for all the animals, IAnimal. And it's implementation, CAnimal. // ============================================================ class IAnimal : public IUnknown { public: // IUnknown methods... STDMETHOD (QueryInterface)(REFIID, LPVOID *) PURE; STDMETHOD_ (ULONG, AddRef)() PURE; STDMETHOD_ (ULONG, Release)() PURE; // IAnimal methods... STDMETHOD (Eat)(FOOD *) PURE; STDMETHOD (Shit)() PURE; }; // ============================================================ class CAnimal : public IAnimal { public: HRESULT QueryInterface(REFIID, LPVOID *); ULONG AddRef(); ULONG Release(); HRESULT Eat(FOOD *); HRESULT Shit(); }; Then I thought, hmm... why not I start by makin' a chicken object? So there I have it, IChicken, and its implementation, CChicken. Now it is obvious that IChicken is derived from IAnimal, so there you go... // ============================================================ class IChicken : public IAnimal { public: // IUnknown methods... STDMETHOD (QueryInterface)(REFIID, LPVOID *) PURE; STDMETHOD_ (ULONG, AddRef)() PURE; STDMETHOD_ (ULONG, Release)() PURE; // IAnimal methods... STDMETHOD (Eat)(FOOD *) PURE; STDMETHOD (Shit)() PURE; // IChicken specific methods... STDMETHOD (Run)(INT) PURE; }; Now the problem comes. What would the CChiken implementation be? It should be derivin' from IChicken because it is supposed to implement the IChicken interface. But then it should also physically be derived from CAnimal since all the implementations have gone into CAnimal (Eat and Shit methods). So how should I start declarin' my CChiken class? Derived both from IChicken and CAnimal?! Hmmm... I'm waitin' for any hints that I might get, and thanks so much for your time. [edited by - HaywireGuy on May 20, 2004 10:16:57 PM]
Advertisement
What is the reason for IChicken? CChicken could implement IAnimal via deriving from CAnimal. There is no difference between IAnimal and IChicken.

Kuphryn

LOL

..(Eat)()..
..(Shit()..

If you make CChicken : CAnimal, you'll have the IAnimal stuff too.


--
You're Welcome,
Rick Wong
- Google | Google for GameDev.net

[edited by - Pipo DeClown on May 20, 2004 12:10:41 PM]
kuphryn:

// IChicken specific methods...
STDMETHOD (Run)(INT) PURE;

IChicken provides a Run method.


[edited by - BlackHC on May 20, 2004 12:09:51 PM]
I do know that I don't know anything.
CChicken : CAnimal, IChicken {} would not work? unsure..



If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

I would say that you''re thinking about it wrong ... I don''t see any reason for having the IChicken interface. Just implement the run Method in CChicken : CAnimal. If for some reason you need wild and new varieties of chicken, you can just CFunkyChicken : CChicken

Joel Martinez
http://www.codecube.net/
Joel Martinez
http://codecube.net
[twitter]joelmartinez[/twitter]
If CChicken inherits CAnimal as it is I think the compiler will bitch at ya for inheriting IAnimal twice, so if you really must inherit the implementation you could make IChicken inherit IUnknown instead of IAnimal and CChicken could then inherit CAnimal without any problems. The annoying part about this is that given an IChicken pointer you would need to QI for IAnimal to access those methods.
Just notice that you are defining you interfaces to have private member functions instead of public. Usually when working with COM people either use struct (as all the member functions should be public) or you can use the interface #define in the com headers (which is a #define for struct but makes it more clear that you are using it for a COM interface).
I posted the question before I slept last night, and wow, when
I woke up I saw these many replies!! First, thanks guy, for
helpin' me. Now... one at a time:

Kuphryn, BlackHC,

Yes... IChicken has an additional method "Run". So CChicken has
to implement this "Run" method in it.

Pipo DeClown,

Hey how have you been dude? Anyway, like I said, CChicken has
another new method called "Run", so I not only needed stuff
from IAnimal, I need those from IChicken too.

davepermen,

That's what I thought of too, but I'm not sure will the final
VTable be screwed up because of this weird way of inheritance.

joelmartinez,

My client application needs to get an instance of CChicken too,
without IChicken interface, my client would be interactin' with
IAnimal directly, which does not tell the client that "Run"
method is supported in CChicken. As for the case of funky
chicken, I'll need IFunkyChicken for the same reason

Fragmo,

Okay, that's one way, but it does not change things much.
First, CChicken needs those implementations which have gone
into CAnimal (ie. Eat, Shit). Second, it needs to derive from
IChicken so that the "Run" method is exposed. So I'm not sure
if I'll end up with this...

class CChicken : public IChicken, public CAnimal.

Will it work?

Polymorphic OOP,

Thanks man, I've forgotten those "public" in all my classes,
I've modified the post






[edited by - HaywireGuy on May 20, 2004 10:16:03 PM]
CAnimal should be a template, and it should inherit from this template parameter

template<class Interface = IAnimal>struct CAnimal : Interface   {   };struct CChicken : CAnimal<IChicken>   {   };
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement