Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Interface inheritance

Reply
Thread Tools

Interface inheritance

 
 
Martin
Guest
Posts: n/a
 
      11-05-2004
Hi,

Suppose I have 3 interfaces :

IStats
IEngine
ICreateCar

each derives from IUnknown

then when I query object that implements those interfaces for IUnknown
interface I need to do two casts:

return (IUnknown*)(IStats*)this;

Is there no difference if I would write : (IUnknown*)(IEngine*)this ? these
are pure abstract interfaces, only pure virtual methods, suppose I had there
some data, would it then make any difference?

Thanks for any help, my COM book just dont get too deep with C++ OOP.


 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      11-05-2004
Martin posted:

> Hi,
>
> Suppose I have 3 interfaces :


By "interface", I'm assuming you mean:

A class,

- which has no member variables or member objects.

- which has no member functions, except pure virtual ones.

- which is not derived from any class which qualifies as an
"interface".


> IStats
> IEngine
> ICreateCar
>
> each derives from IUnknown
>
> then when I query an object that implements those interfaces for IUnknown
> interface I need to do two casts:
>
> return (IUnknown*)(IStats*)this;


By "query", I'm assuming you mean either

A) Access a member variable or member object.

B) Access a member function.


You don't need those casts. Take them out and (try) compile it. If the
classes trully are derived from one another, then it will work.


> Is there no difference if I would write : (IUnknown*)(IEngine*)this ?
> these are pure abstract interfaces, only pure virtual methods, suppose
> I had there some data, would it then make any difference?


Again casting is not necessary if the classes are derived from one another.


Let's say for instance that the casts *were* necessary, ie. that
"IPowerfulEngine" was not actually derived from "IEngine", like as follows:

class IEngine : public IUnknown {};

class IPowerfulEngine {};


Even in this case, in writing:

SomeFunc( (IUnknown*)(IEngine*)&powerful_engine_object );

You're just wasting your time typing.

What you *could* have written was simply:

(IUnknown*)&powerful_engine_object;

or better:

reinterpret_cast< IUnknown* > (&powerful_engine_object);


-JKop
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      11-05-2004

"JKop" <(E-Mail Removed)> wrote in message
news:ZpIid.40967$(E-Mail Removed)...
> Martin posted:
>
> > Hi,
> >
> > Suppose I have 3 interfaces :

>
> By "interface", I'm assuming you mean:
>
> A class,
>
> - which has no member variables or member objects.
>
> - which has no member functions, except pure virtual ones.
>
> - which is not derived from any class which qualifies as an
> "interface".
>
>
> > IStats
> > IEngine
> > ICreateCar
> >
> > each derives from IUnknown
> >
> > then when I query an object that implements those interfaces for

IUnknown
> > interface I need to do two casts:
> >
> > return (IUnknown*)(IStats*)this;

>
> By "query", I'm assuming you mean either
>
> A) Access a member variable or member object.
>
> B) Access a member function.
>
>
> You don't need those casts. Take them out and (try) compile it. If the
> classes trully are derived from one another, then it will work.
>


You do need (IStats*) because IStats, IEngine and ICreateCar all derive from
IUnknown so the conversion is ambiguous without any casts. But you don't
need the (IUnknown*) cast.

>
> > Is there no difference if I would write : (IUnknown*)(IEngine*)this ?
> > these are pure abstract interfaces, only pure virtual methods, suppose
> > I had there some data, would it then make any difference?

>
> Again casting is not necessary if the classes are derived from one

another.
>


The OP has multiple IUnknown objects, so if those objects had some state
then it could make a difference.

john


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-05-2004
>
> Thanks for any help, my COM book just dont get too deep with C++ OOP.
>


Buy 'Understanding COM' by Don Box. It sounds like it is exactly what you
need. It is also the COM book for C++ programmers.

john


 
Reply With Quote
 
Martin
Guest
Posts: n/a
 
      11-05-2004

Użytkownik "JKop" <(E-Mail Removed)> napisał w wiadomości
news:ZpIid.40967$(E-Mail Removed)...

> By "interface", I'm assuming you mean:
>
> A class,
>
> - which has no member variables or member objects.
> - which has no member functions, except pure virtual ones.
> - which is not derived from any class which qualifies as an
> "interface".


Actually it can derive from other interfaces, and I think this is why those
casts are needed, this is multiple inheritance with some of the base classes
being the same (like IUnknown).

> By "query", I'm assuming you mean either
>
> A) Access a member variable or member object.
>
> B) Access a member function.


By query I mean to cast object to one of its base classes (interfaces)

> You don't need those casts. Take them out and (try) compile it. If the
> classes trully are derived from one another, then it will work.
>
>
>> Is there no difference if I would write : (IUnknown*)(IEngine*)this ?
>> these are pure abstract interfaces, only pure virtual methods, suppose
>> I had there some data, would it then make any difference?

>
> Again casting is not necessary if the classes are derived from one
> another.
>
>
> Let's say for instance that the casts *were* necessary, ie. that
> "IPowerfulEngine" was not actually derived from "IEngine", like as
> follows:
>
> class IEngine : public IUnknown {};
>
> class IPowerfulEngine {};
>
>
> Even in this case, in writing:
>
> SomeFunc( (IUnknown*)(IEngine*)&powerful_engine_object );
>
> You're just wasting your time typing.
>
> What you *could* have written was simply:
>
> (IUnknown*)&powerful_engine_object;


Yes, but base classes (interfaces) of powerful_engine_object class all
derive from IUnknown. So I think compiler just wants clarification to
resolve ambiguous situation. Here is some code :

//
class IStats : public IUnknown
{};
class IEngine : public IUnknown
{};
class CoCar : public IEngine, IStats
{};

//
STDMETHODIMP CoCar::QueryInterface(REFIID riid, void **ppvInt)
{
*ppvInt = NULL;
if (riid == IID_IUnknown)
{
*ppvInt = (IUnknown*)(IEngine*)this; //this is ok

//*ppvInt = (IUnknown*)this;
// gives : error C2594: 'type cast' : ambiguous conversions from
'CoCar *const ' to 'IUnknown *'
}
else if (riid == IID_IStats)
{
*ppvInt = (IStats*)this;
}
else if (riid == IID_IEngine)
{
*ppvInt = (IEngine*)this;
}
((IUnknown*)(*ppvInt))->AddRef();
return S_OK;
}


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      11-05-2004

>> - which is not derived from any class which qualifies as an
>> "interface".



Should've written:

- which is not derived from any class which does not qualifiy as an
"interface".


-JKop
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      11-05-2004
Martin wrote:
> Hi,
>
> Suppose I have 3 interfaces :
>
> IStats
> IEngine
> ICreateCar
>
> each derives from IUnknown
>
> then when I query object that implements those interfaces for IUnknown
> interface I need to do two casts:
>
> return (IUnknown*)(IStats*)this;
>
> Is there no difference if I would write : (IUnknown*)(IEngine*)this ? these
> are pure abstract interfaces, only pure virtual methods, suppose I had there
> some data, would it then make any difference?
>
> Thanks for any help, my COM book just dont get too deep with C++ OOP.
>
>


As a side note, do not, repeat DO NOT use C-style casts in this sort of
situation. You need to use static_cast<> or dynamic_cast<>.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Struct inheritance against class inheritance johnsonlau C++ 1 07-21-2008 04:58 PM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments