   Shouldn't interfaces deriving from IUnknown derive it virtually
  I have never implemented any COM interface before, but I do not get this idea why interfaces do not derive from IUnknown virtually. Suppose two interfaces derive from IUnknown and both interfaces are implemented by a class, IUnknown will be derived twice (via multiple inheritance). How does that work out considering there will be one reference counter for that class?Shouldn't the public virtual keyword be used rather than the public?Any ideas?Thanks,obhi   
  IUnknown is an interface; there is no associated implementation. The number of times IUnknown appears as part of the vtable does not have anything to do with the number of times that you need to implement the functions IUnknown contains. If you have two vtables in your object, each with IUnknown up front you can still implement the functions once. The two IUnknown parts will reference the same functions. Furthermore, COM requires that the function pointers for an interface that derives from another interface must include the base function pointers at the beginning of the derived interface function pointer table. This means that each and every COM function table must have as the first three entries QueryInterface, AddRef and Release without exception. The easiest way to manage this when implementing a COM interface in C++ is to derive non-virtually from IUnknown.   ;  That makes sense considering the pointers in vtable can always point to the single implementation of AddRef,etc.And since only pure interfaces are derived, so no problems whatsoever.Thanks :)obhi   ;  One more thing...you said Quote:Furthermore, COM requires that the function pointers for an interface that derives from another interface must include the base function pointers at the beginning of the derived interface function pointer table. This means that each and every COM function table must have as the first three entries QueryInterface, AddRef and Release without exception. The easiest way to manage this when implementing a COM interface in C++ is to derive non-virtually from IUnknown.Isn't this the case with all classes deriving from a base class with virtual functions? The first n entries of the vtable should be the n number of virtual functions present in the base class, otherwise typecasting couldn't work. Correct me if I'm wrong.   ;  It may or may not be the case when virtual inheritance is used.   ;  Yes, virtual inheritance will alter the vtable layout and proper casting will require either the derived class pointer.Now, I'm really confused, so why is QueryInterface needed if there is no virtual inheritance, cant the compiler calculate the address shift at compile time if we directly cast an interface to another.Now, I just tried this code:class Base{virtual void msg() = 0;};class Intf1 : public Base{std::string msg;public:Intf1() {msg = "Intf1!";}virtual void msg_intf1() {std::cout << "msg_intf1!??? " << msg << std::endl;}};class Intf2 : public Base{std::string msg;public:Intf2() {msg = "Intf2!";}virtual void msg_intf2() {std::cout << "msg_intf2!??? " << msg << std::endl;}};class FinalClass : public Base,public Intf1,public Intf2{public:virtual void msg(){std::cout << "final msg" << std::endl;}};And I got predictable warnings: C4584: 'FinalClass' : base-class 'Base' is already a base-class of 'Intf1'C4584: 'FinalClass' : base-class 'Base' is already a base-class of 'Intf2'So, does COM actually suppress these warnings (sorry for this silly question) and is it ok to do so?   ;  The thing I said about QueryInterface is because I implemented a similar model in a project a while ago, where the derived class stored the 'this' pointer in the only base class implementing an interface similar to IUnknown, and deriving other interfaces (but not implementing them, the class implemented them). The QueryInterface served the purpose of down casting that pointer to any respective interface that was queried and supported by the derived class. IUnknown was derived virtualy so static_cast on any pointer would not have worked, so this model was used. I presumed COM did the same thing, i m not sure though.Unless of course there could be a definite purpose for QueryInterface to see if that interface is actually supported rather than bluntly type casting a pointer. But lets say if we do knew that interface was supported, would a type cast from one interface to the queried one do the same job? (considering there was no virtually derived class/interface)   ;  And thanks for your kind reply's :)   ;Quote:Original post by obhiNow, I'm really confused, so why is QueryInterface needed if there is no virtual inheritance, cant the compiler calculate the address shift at compile time if we directly cast an interface to another.No, because you don't have access to the actual layout of the implementation. You know the layout of ISomeInterface, but you don't know what interfaces the particular implementation of ISomeInterface exposes, or in what order.   ;Quote:Original post by SneftelQuote:Original post by obhiNow, I'm really confused, so why is QueryInterface needed if there is no virtual inheritance, cant the compiler calculate the address shift at compile time if we directly cast an interface to another.No, because you don't have access to the actual layout of the implementation. You know the layout of ISomeInterface, but you don't know what interfaces the particular implementation of ISomeInterface exposes, or in what order.Ahh! sure! How would the compiler know of the order in which the interfaces are inherited. Clears a lot of doubt :)Thanks again   