473,770 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*)(ISt ats*)this;

Is there no difference if I would write : (IUnknown*)(IEn gine*)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.
Jul 22 '05 #1
6 4889
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*)(ISt ats*)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*)(IEn gine*)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
"IPowerfulEngin e" was not actually derived from "IEngine", like as follows:

class IEngine : public IUnknown {};

class IPowerfulEngine {};
Even in this case, in writing:

SomeFunc( (IUnknown*)(IEn gine*)&powerful _engine_object );

You're just wasting your time typing.

What you *could* have written was simply:

(IUnknown*)&pow erful_engine_ob ject;

or better:

reinterpret_cas t< IUnknown* > (&powerful_engi ne_object);
-JKop
Jul 22 '05 #2

"JKop" <NU**@NULL.NULL > wrote in message
news:Zp******** ***********@new s.indigo.ie...
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*)(ISt ats*)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*)(IEn gine*)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
Jul 22 '05 #3
>
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
Jul 22 '05 #4

U¿ytkownik "JKop" <NU**@NULL.NULL > napisa³ w wiadomo¶ci
news:Zp******** ***********@new s.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.

Is there no difference if I would write : (IUnknown*)(IEn gine*)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
"IPowerfulEngin e" was not actually derived from "IEngine", like as
follows:

class IEngine : public IUnknown {};

class IPowerfulEngine {};
Even in this case, in writing:

SomeFunc( (IUnknown*)(IEn gine*)&powerful _engine_object );

You're just wasting your time typing.

What you *could* have written was simply:

(IUnknown*)&pow erful_engine_ob ject;


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::QueryInt erface(REFIID riid, void **ppvInt)
{
*ppvInt = NULL;
if (riid == IID_IUnknown)
{
*ppvInt = (IUnknown*)(IEn gine*)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*)(*p pvInt))->AddRef();
return S_OK;
}
Jul 22 '05 #5
- 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
Jul 22 '05 #6
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*)(ISt ats*)this;

Is there no difference if I would write : (IUnknown*)(IEn gine*)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<>.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
13273
by: Tom Evans | last post by:
My basic question: If I have a specific interface which I know is going to be implemented by a number of classes, but there is no implementation commonality between them, what is the preferred form for this in Python? In a staticly typed language like c++ or java, I'd describe the interface first, then create the classes eithered derived from that ABC or implementing that interface (same thing really). This was the first thing I...
4
8204
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by making my contnent dlls implement an interface created in vb6. The viewer application is bound to this interface. This way, I am able to add Content without redeploying the dlls (I just have to add the new dlls). I want to write new content for...
4
2109
by: christopher diggins | last post by:
A feature that I find signficantly missing in C# is the ability to write functions in interfaces that can call other functions of the interface. Given an interface ISomeInteface the only way we can write a general purpose function to operate on all objects which implement that interface is through the following style of declaration (in the following code snippets i is an interface variable of type ISomeInterface) : SomeStaticClass {...
21
13847
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered by Sets. - ICollection cannot decide containment - IList promises indexability by the natural numbers, which is not achievable (since i hash elements, not sort them). - IDictionary is definatly not setlike. Although I can, of course, define...
7
12877
by: Hazz | last post by:
Are there any good references/articles/books which provide clarity toward my insecurity still on deciding how to model a complex system? I still feel uncomfortable with my understanding, even though I have worked with these systems on when to decide to use interfaces (and how they should be developed) as opposed to or complemented by the use of inheritance from base classes. If I am thinking from the point of view of some specific activity...
10
2979
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract class. The interface has implied abstract methods/properties and derived classes can inherit multiple interfaces. The interface properties/methods have no implementation. Besides definitions of the two, what are some conceptual reasons to use...
6
6080
by: John Salerno | last post by:
I understand how they work (basically), but I think maybe the examples I'm reading are too elementary to really show their value. Here's one from Programming C#: #region Using directives using System; using System.Collections.Generic; using System.Text;
12
2844
by: Meya-awe | last post by:
I am puzzled, what is the purpose of an interface? How does it work, what i mean is how does the compiler treats this? Why when we talk about separating user interface from business logic, an interface is declared, what is it's purpose? thanks, BRAMOIN *** Sent via Developersdex http://www.developersdex.com ***
4
20640
by: Raja Chandrasekaran | last post by:
Hai friends, I really wonder, If the interface does not have any definition, Y do we need to use interface. You can then only we can use Multiple inheritance. I really cant understand, Just for declararion y do we need to use interface. Anyhow the method name and definition ll be in the derived class. Instead of that we can do all code in the derived class itself right...? Then y these concept came. If anybody know, please explain me...
52
20909
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member 'UselessJunkForDissassembly.IInvocableInternals.OperationValidate(string)' C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.