views:

91

answers:

3

Ok, I know what a interface is, but since I got into C and working with COM objects (Component Object Model), it seems an interface in COM is a little different from the interface I know of.

So what I am trying to do is bridge the gaps here cause since I been learning C, alot of things have been sounding very familiar to me but are not exactly what they seem.

The interface I know of are like contracts. They are objects that have only method declarations, with no body. All classes that implement an interface must include the methods of the interface.

The interface I hear about in COM seems to be just pointers. They can not retrieve objects directly but only can retrieve objects through the means of a method. Is this what a COM Interface is ?? If so, then why did they give them the same names if they are completely different.

Also I just wanted to add that headers in C++ kind of remind me of the C# Interfaces. Not sure if their are any relations. But anyways, I am just trying to clear that up.

+2  A: 

The interface I know of are like contracts. They are objects that have only method declarations, with no body. All classes that implement an interface must include the methods of the interface.

An C# interface would be an empty abstract class in C++.

devoured elysium
I updated the question. I meant as in the COM (component object model)
numerical25
+1  A: 

The term interface is generally just a concept for some kind of "contract" in software development. As C has no object-orientation built-in (it's a procedural language) the term "interface" means something different than in Java, for example, where they have a special meaning as a language construct. In C++ there's also no explicit interface keyword / language construct but you can get similar semantics by defining pure abstract classes (classes that only contain pure virtual methods).

Example for an "interface" in C++:

class Comparable {
public:
  virtual int compareTo(Comparable const * const other) const = 0; // <- the "= 0" indicates a _pure_ virtual method 
};
Christopher Bertels
Well I guess the one I am talking about is in the COM. Like IUNKNOWN .
numerical25
A: 

There are definitely low-level details they have in common. An interface is a list of function pointers in both languages. More commonly implemented in C++ because most compilers already implement virtual functions as a "v-table". A one-to-one match with a COM dispatch table.

Where they start to diverge is the methods of IUnknown. A COM interface has the AddRef and Release methods to take care of memory managed, using reference counting. A non-issue in C# because the garbage collector take care of it.

And COM doesn't support inheritance. Not an issue in C#, if you want to obtain an interface pointer then you simply cast the object reference. It has to be done explicitly in COM with the IUnknown::QueryInterface() method. There is some support in COM for object re-use, implemented by aggregation. It is so obtuse that few ever implement it.

Hans Passant
What do you mean by "And COM doesn't support inheritance"? COM supports single inheritance at the interface level. Did you mean it doesn't support a certain style of inheritance or something?
Luke
No, it aggregates the methods of "base" interface, they all have to be re-implemented. C++ *does* support inheritance, making it a lot easier to implement the interface.
Hans Passant