Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Interface inheritance
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.



Report this thread to moderator Post Follow-up to this message
Old Post
Martin
11-05-04 01:56 PM


Re: Interface inheritance
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

Report this thread to moderator Post Follow-up to this message
Old Post
JKop
11-05-04 01:56 PM


Re: Interface inheritance
"JKop" <NULL@NULL.NULL> wrote in message
news:ZpIid.40967$Z14.15884@news.indigo.ie...
> Martin posted:
> 
>
> 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".
>
> 
IUnknown 
>
> 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.

> 
>
> 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



Report this thread to moderator Post Follow-up to this message
Old Post
John Harrison
11-05-04 01:56 PM


Re: Interface inheritance
>
> 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



Report this thread to moderator Post Follow-up to this message
Old Post
John Harrison
11-05-04 01:56 PM


Re: Interface inheritance
Użytkownik "JKop" <NULL@NULL.NULL> napisał w wiadomości
news:ZpIid.40967$Z14.15884@news.indigo.ie...

> 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.
>
> 
>
> 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;
}



Report this thread to moderator Post Follow-up to this message
Old Post
Martin
11-05-04 01:56 PM


Re: Interface inheritance
 


Should've written:

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


-JKop

Report this thread to moderator Post Follow-up to this message
Old Post
JKop
11-05-04 08:56 PM


Re: Interface inheritance
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 ? thes
e
> are pure abstract interfaces, only pure virtual methods, suppose I had the
re
> 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<>.

Report this thread to moderator Post Follow-up to this message
Old Post
red floyd
11-05-04 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

C++ archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:25 AM.

 

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.