Krishna Chaithanya

Hi,

I am working on C++ in COM based environment. Currently, in a component, I have a class 'B'. And classes D1 and D2 are derived from this class. I have some virtual functions in 'B' which are overridden in D1 and D2 for their purpose. None of these classes are now used by any other components in the system and so, there was no interface and coclass defined for these.

So, when I want to use a particular class, I instantiate it and use a pointer variable of 'B' to use the functions of that specific derived class. Now, I have a requirement to expose these classes to outside components in the system. For this, I have to write 3 new interfaces for these 3 new classes and different co-classes and different implementation classes. If I have three different implementation classes, it results in duplication of code since D1 and D2 should also implement the functions of 'B' again.

So, my doubt is, is there any way in COM to avoid this duplication and use the same code written in 'B'. To explain it more clearly,

The present scenario:

B is having some virtual functions func(), func1(), func2().

D1 will override func2() and implement new functions func3(), func4().

D2 will override func1() and implement new functions func5(), func6().

The scenario if these have to exposed to other components:

Three new interface are added each one for B, D1 and D2.

Now, from the client, I will do a QueryInterface and get an interface pointer to D1 interface. Using this interface pointer, I can only call func3() and func4(). But, I can't call func1() from here. If I have to call func1(), I should implement it again in D1 interface with the same implementation in B interface.

Hope I am clear in what I have explained. Kindly revert back if you don't understand something.

So, kindly suggest if there is any way in COM to do this requirement without any duplication of code.

Thanks and Regards,

krishna



Re: Visual C++ General A doubt on inheritance in COM and C++

Aleksandr Tokarev

No, You don't clear.

As for me C++ is international languge in this forum.

#define interface class

interace IB: public IUnknow

{

public:

void func();

void func1();

void func2();

};

interface ID1: public IUnknow

{

public:

void func3();

void func4();

};

interface ID2: public IUnknow

{

public:

void func5();

void func6();

};

ID1* d1;

CreateObject(__uuid(D1), &d1);

d1->func5(); //OK

d1->func6(); //OK

d1->func1(); //error !..

Is this model right

You can resolve it...

1. Call QueryInterface for IB:

IB* b;

d1->QueryInterface(__uuid(IB), &b);// OK becase ID1 derived from IB !

b->func1();

b->func();

b->func2();

2. Derive ID1 from IB directly:

interface ID1: public IB /*IUnknow*/ // in IDL its possible!

{

public:

void func3();

void func4();

};

then calling d1->func1() will successful!

d1->func6(); //OK

d1->func1()// OK!

If my model is not coincide with your.

Write your...





Re: Visual C++ General A doubt on inheritance in COM and C++

Krishna Chaithanya

Hi Aleksandr,

Thanks for your reply. Since you said I was not clear, I am explaining here with the example.

This is the present situation in my code. (This code is a part of COM component but not exposed to any other components).

class B {

public:

virtual void func();

virtual void func1();

virtual void func2();

}

class D1 : public B {

void func3();

void func4();

void func2() { //do something...};

}

class D2 : public B {

void func5();

void func6();

void func1() { //do something...};

}

int main() {

B* b;

//Based on the requirement, I need to create the specific object. Here, I create an object for D1.

b = new D1;

b->func3(); //OK. Should call D1's func3().

b->func4(); //OK. Should call D1's func4().

b->func2(); //OK. Should call D1's func2() since overridden.

b->func1(); //OK. Should call B's func1()

b->func(); //OK. Should call B's func()

b->func5(); //Error. Should not work.

b->func6(); //Error. Should not work.

}

So, here, I was able to call func3, func4, func1 and most importantly the correct func2().

Now, coming to the situation after I change these to interfaces and exposing to other components.

interace IB: public IUnknown

{

public:

void func();

void func1();

void func2();

};

interface ID1: public IUnknown

{

public:

void func3();

void func4();

};

interface ID2: public IUnknown

{

public:

void func5();

void func6();

};

Suppose that all these interfaces are supported by my coclass(CA).

And I write this code from some client component.

ID1* d1;

CreateObject(__uuid(D1), &d1);

d1->func3(); //OK. Should be fine. It should call the func3() of the implementation class for ID1 interface.

d1->func4(); //OK. Should be fine. It should call the func4() of the implementation class for ID1 interface.

d1->func2(); //Error. It will not work since func2 is not overridden in ID1's implementation class.

d1->func1(); //Error. func1() is only implemented in IB's implementation class. Since it is not implemented in ID1's implementation class, this will not work. This is my main requirement.

d1->func(); //Error. func1() is only implemented in IB's implementation class. Since it is not implemented in ID1's implementation class, this will not work. This is my main requirement.

So, if you can see clearly, I am able to call func2, func1 also with the same pointer variable in my present implentation. But, if I make them separate interfaces, I am not able to create an object which is able to call func2, func1 and func.

As you suggested to go by interface inheritance, the situation can improve to this extent.

ID1* d1;

CreateObject(__uuid(D1), &d1);

d1->func3(); //OK. Should be fine. It should call the func3() of the implementation class for ID1 interface.

d1->func4(); //OK. Should be fine. It should call the func4() of the implementation class for ID1 interface.

d1->func2(); //OK. Should be fine. It should call the func2() of the implementation class for IB interface.

d1->func1(); //OK. Should be fine. It should call the func1() of the implementation class for IB interface.

d1->func(); //OK. Should be fine. It should call the func() of the implementation class for IB interface.

But, here again, my actual purpose is not yet done. In my present implementation, I have virtual function func2() of class B which was overridden in D1. And whenever I create an object of D1, I was able to call this function with a pointer to B.

I want to know if this support is there in COM. I doubt it is not possible.

Suppose the name of coclass which is supporting these interfaces in CA. And my implementation class(CAImpl) should be derived from this CA. So, even though I can declare func2() in IB, ID1 and ID2, the implementation of func2() will be in CAImpl and can be only one implementation.

I am not sure if this can work if I use three different coclasses to support my three interfaces IB, ID1 and ID2. But, this can be tedious and requires generation of many implementation classes as well.

Kindly, let me know if this is possible in COM.

If I am still not clear, please let me know and I will try my best to be clear.

Thanks and Regards,

krishna




Re: Visual C++ General A doubt on inheritance in COM and C++

Aleksandr Tokarev

Unfotuntly it's not clear for me too.

Do you would like to obtain a pointer to IB* and call method via IB* pointer

Like in your main() function

int main() {

B* b;

//Based on the requirement, I need to create the specific object. Here, I create an object for D1.

b = new D1;

b->func3(); //OK. Should call D1's func3().

b->func4(); //OK. Should call D1's func4().

b->func2(); //OK. Should call D1's func2() since overridden.

b->func1(); //OK. Should call B's func1()

b->func(); //OK. Should call B's func()

b->func5(); //Error. Should not work.

b->func6(); //Error. Should not work.

}

Then if ID1 derived from IB it is possible implicite casting to IB*!

int main() {

ID1* d1;

IB* b = d1;

CreateObject(__uuid(D1), &d1);

b->func3(); //OK. Should call D1's func3().

b->func4(); //OK. Should call D1's func4().

b->func2(); //OK. Should call D1's func2() since overridden.

b->func1(); //OK. Should call B's func1()

b->func(); //OK. Should call B's func()

b->func5(); //Error. Should not work.

b->func6(); //Error. Should not work.

}





Re: Visual C++ General A doubt on inheritance in COM and C++

Krishna Chaithanya

Hi Aleksandr,

I will try to get more into the example.

Here are the interface declarations and coclass declarations.

I am just writing only func2() here to avoid confusion.

interace IB: public IUnknown

{

public:

void func2();

};

interface ID1: public IB

{

public:

void func2(); //Can I write this Can I override this base class function

};

interface ID2: public IB

{

public:

void func2(); //Can I write this Can I override this base class function

};

Now the coclass(CA) declaration which supports these interfaces.

coclass CA {

supports IB;

supports ID1;

supports ID2;

}

Now, my actual implementation of the interface function func2(). In order for this interface call to work, I have to derive my Implementation class from CA. So, here is the declaration of my Implementation class.

CAImpl : public CA {

HRESULT func2() {

//do something...

}

}

As I said in bold above, even if it is possible to declare func2() in ID1 and ID2, the implementation class(CAImpl) can only define one func2() since this class is derived from CA which is supporting all the interfaces.

So, my question is, is there a way to give multiple implementation to func2() So, that using a pointer to base class (IB), I can call the correct implementation of ID1 or ID2

This was easily possible in C++ since the concept is just overriding a virtual function of the base class. But in COM, since all the implementations are coming into a single file, it is not possible to override the function.

Kindly post your thoughts now.

Thanks and Regards,

krishna




Re: Visual C++ General A doubt on inheritance in COM and C++

Aleksandr Tokarev

interace IB: public IUnknown

{

public:

void func2();

};

interface ID1: public IB

{

public:

void func2(); //Can I write this Can I override this base class function

// NO YOU CAN't

};

interface ID2: public IB

{

public:

void func2(); //Can I write this Can I override this base class function

// NO YOU CAN't

};

May be better to make this:

coclass CAD1 {

supports IB;

supports ID1;

}

coclass CAD2 {

supports IB;

supports ID2;

}

ID1* d1;

IB* b = d1;

CreateObject(__uuid(CAD1), &d1);

b->func2(); //OK. Should call D1's func2() since overridden.

ID2* d2;

IB* b = d2;

CreateObject(__uuid(CAD2), &d2);

b->func2(); //OK. Should call D2's func2() since overridden.

You can make multiple implementation of B2::func2() but it's not good practice!!!

  1. This will confuse users of you class.
  2. You contradict nature of you object model (you have 2 different object with differenct behaviour, not one object with diffrent interfaces).
  3. You need additional not very clear changes in your class structure.

In C++ model you have 2 diffrent classes and create them separately! In COM model you have 1! Why





Re: Visual C++ General A doubt on inheritance in COM and C++

Krishna Chaithanya

Hi Aleksandr,

I am not intentionally making a single class in COM model but that is how COM works. Since my coclass CA is supporting all the three interfaces, then there can be only one implementation class(CAImpl) for CA. This is the reason it is becoming as once class.

I want to know if it is possible to make it to two classes in COM.

Now, lets go by the model you have explained.

coclass CAD1 {

supports IB;

supports ID1;

}

coclass CAD2 {

supports IB;

supports ID2;

}

and let the interface definitions be in this way..

interace IB: public IUnknown

{

public:

void func1();

};

interface ID1: public IB

{

public:

void func2();

};

interface ID2: public IB

{

public:

void func3();

};

Now, coming to implementation classes of the coclasses,

CAD1Impl : CAD1 {

func1() { //do something... }; //First implementation of func1()

func2() { //do something... };

}

CADImpl2 : CAD2 {

func1() { //do something...}; //second implementation of func1()

func3() { //do something...};

}

Here, I should be duplicating the code for func1() without any purpose. This is not my desired model.

In C++, if func1() is a virtual function, then if the derived class doesn't override the function, the base function can be called. And there is no duplication of code. So, kindly let me know if it is possible in COM.

I have a model of my own. Can you please let me know if this is feasible in COM

Here are the interface definitions:

interace IB: public IUnknown

{

public:

void func1();

};

interface ID1: public IB

{

public:

void func2();

};

interface ID2: public IB

{

public:

void func3();

};

Here are the coclass definitions:

coclass CAB {

supports IB;

}

coclass CAD1 {

supports ID1;

}

coclass CAD2 {

supports ID2;

}

And here are my implementation classes:

CABImpl : CAB {

func1() { //do something... };

}

CAD1Impl : CABImpl, CAD1 {

func2() { //do something... };

}

CAD2Impl : CABImpl, CAD2 {

func3() { //do something... };

}

Now, If I write this code, what is the output

ID1* d1;

IB* b = d1;

CreateObject(__uuid(CAD1), &d1);

b->func1(); //Will this work

b->func2(); //OK. It will work.

ID2* d2;

IB* b = d2;

CreateObject(__uuid(CAD2), &d2);

b->func1(); //Will this work

b->func3(); //OK. It will work.

Here, the implementation is only one. But, it can be called from two different objects.

Kindly let me know if this model works fine.

Thanks for your patience. I am learning a lot from the discussion!

I have a similar doubt in COM. If this model can work, I can work that also.

Thanks and Regards,

krishna





Re: Visual C++ General A doubt on inheritance in COM and C++

Aleksandr Tokarev

interace IB: public IUnknown

{

public:

void func1();

};

interface ID1: public IB

{

public:

void func2();

};

interface ID2: public IB

{

public:

void func3();

};

Now, coming to implementation classes of the coclasses,

CAD1Impl : CAD1 {

func1() { //do something... }; //First implementation of func1()// You can implement it in Base class and derive this class from the base.

func2() { //do something... };

}

CADImpl2 : CAD2 {

func1() { //do something...}; //second implementation of func1()// You can implement it in Base class and derive this class from the base.

func3() { //do something...};

}

Here, I should be duplicating the code for func1() without any purpose. This is not my desired model.

In C++, if func1() is a virtual function, then if the derived class doesn't override the function, the base function can be called. And there is no duplication of code. So, kindly let me know if it is possible in COM.

//Below you gave a corect model do not need double implementation.

I have a model of my own. Can you please let me know if this is feasible in COM

Here are the interface definitions:

interace IB: public IUnknown

{

public:

void func1();

};

interface ID1: public IB

{

public:

void func2();

};

interface ID2: public IB

{

public:

void func3();

};

Here are the coclass definitions:

coclass CAB { // Do you need interface on B class

supports IB;

}

coclass CAD1 {

supports ID1;

}

coclass CAD2 {

supports ID2;

}

And here are my implementation classes:

CABImpl : CAB {

func1() { //do something... };

}

CAD1Impl : CABImpl, CAD1 {

func2() { //do something... };

}

CAD2Impl : CABImpl, CAD2 {

func3() { //do something... };

}

Now, If I write this code, what is the output

ID1* d1;

CreateObject(__uuid(CAD1), &d1);//I've changed the order of the lines

IB* b = d1;

b->func1(); //Will this work //Yes this will.

b->func2(); //OK. It will work.

ID2* d2;

CreateObject(__uuid(CAD2), &d2); //I've changed the order of the lines

IB* b = d2;

b->func1(); //Will this work //Yes this will.

b->func3(); //OK. It will work.

Kindly let me know if this model works fine.

No doubt your model will works. There are can be compile time problem cause of class inherence but they resolveable! You implement mine model. I've described it in COM class terms, u described it implementation. So, I cannot agree that it is you own model. Smile

And the last, Component Oriented Model describes rules for interection of you objects with objects of other developers or your objects that makes different task (with other component).

Component Oriented Model (COM) DOES NOT exclude Object Oriented Model, Object Oriented Model take a role of implementation engine for COM interfaces.

COM Interfaces in C++ become just abstract classes(real C++)! There is no magic!

Feel free to ask some question in implementation. And do not forgot mark a post as Answer and helpful (if it is) Smile





Re: Visual C++ General A doubt on inheritance in COM and C++

Krishna Chaithanya

Hi Aleksandr,

Thanks for the information.

I have got to know the point now.

But, I still have some similar doubt in COM.

Here is my interface definition:

interface IB {

void func1();

void func2();

void func3();

}

Here are coclasses definitions:

coclass CAB {

supports IB;

}

coclass CAD1 {

supports IB;

}

coclass CAD2 {

supports IB;

}

And here are my implementation classes:

CABImpl : CAB {

func1() { //do something... };

func2() { //do something... };

func3() { //do something....};

}

CAD1Impl : CABImpl, CAD1 {

func2() { //do something... };

}

CAD2Impl : CABImpl, CAD2 {

func2() { //do something... };

func3() { //do something... };

}

IB* b;

CreateObject(__uuid(CAB), &b);

b->func1(); //Should call CABImpl::func1(). Will this work

b->func2(); //Should call CABImpl::func2() since overridden. Will this work

b->func3(); //Should call CABImpl::func3() since overridden. Will this work

IB* d1;

CreateObject(__uuid(CAD1), &d1);

d1->func1(); //Should call CABImpl::func1() since func1() is not overridden in CAD1Impl. Will this work

d1->func2(); //Should call CAD1Impl::func2() since overridden. Will this work

d1->func3(); //Should call CABImpl::func3() since func1() is not overridden in CAD1Impl. Will this work

IB* d2;

CreateObject(__uuid(CAD2), &d2);

d2->func1(); //Should call CABImpl::func1() since func1() is not overridden in CAD1Impl. Will this work

d2->func2(); //Should call CAD2Impl::func2() since overridden. Will this work

d2->func3(); //Should call CAD2Impl::func3() since overridden. Will this work

I am not able to think how exactly COM interprets this model. I know that every interface class will be abstract class and our implementation class should override all the functions declared in the interfaces. But, I am not sure what happens if that interface is supported by different coclasses and have different implementations Should(Must) I be overriding every function in the interface in all the implementation classes If so, this model will not work.

Once you clear this, I will mark this as answer.

Once again, thanks for your patience!!!

Thanks and Regards,

krishna





Re: Visual C++ General A doubt on inheritance in COM and C++

Aleksandr Tokarev

IB* b;

CreateObject(__uuid(CAB), &b);

b->func1(); //Should call CABImpl::func1(). Will this work // Yes

b->func2(); //Should call CABImpl::func2() since overridden. Will this work // Yes

b->func3(); //Should call CABImpl::func3() since overridden. Will this work // Yes

IB* d1;

CreateObject(__uuid(CAD1), &d1);

d1->func1(); //Should call CABImpl::func1() since func1() is not overridden in CAD1Impl. Will this work // Yes

d1->func2(); //Should call CAD1Impl::func2() since overridden. Will this work // Yes

d1->func3(); //Should call CABImpl::func3() since func1() is not overridden in CAD1Impl. Will this work // Yes

IB* d2;

CreateObject(__uuid(CAD2), &d2);

d2->func1(); //Should call CABImpl::func1() since func1() is not overridden in CAD1Impl. Will this work // Yes

d2->func2(); //Should call CAD2Impl::func2() since overridden. Will this work // Yes

d2->func3(); //Should call CAD2Impl::func3() since overridden. Will this work // Yes

I am not able to think how exactly COM interprets this model.

I know that every interface class will be abstract class and our implementation class should override all the functions declared in the interfaces.// But your implementation derived from CABImpl where all this methods are implemented!

But, I am not sure what happens if that interface is supported by different coclasses and have different implementations

//Like from OOP side its usual virtual function, if it implemented in upper class and not implemented in lower then you code will work with those who in upper, if it is implemented in lower class then those who in lower.

Should(Must) I be overriding every function in the interface in all the implementation classes

// No you shouldn't override EVERY function you can play with inherence in usuall C++ way to implement all need method in base class and specify some method in derived class..

Once you clear this, I will mark this as answer.

// Do not forget markup all helpful posts!





Re: Visual C++ General A doubt on inheritance in COM and C++

Krishna Chaithanya

Hi Aleksandr,

Thanks for your help.

Hopefully, I would be posting few more questions in future for you....

Thanks and Regards,

krishna