COM achieves C ++ skills used by aggregation

xiaoxiao2021-03-06  91

COM aggregation is a means of COM implementation interface multiplexing, which is a valid and rapid method when performing multi-interface forwarding; however, to ensure the language of the interface function, you must use some specific techniques, below The code demonstrates the skills used when COM implements aggregation:

Class Base

{

PUBLIC:

Virtual void output ()

{

Printf ("Base :: Output / N");

}

}

Class Baseex

{

PUBLIC:

Virtual void print ()

{

Printf ("Baseex :: Print / N");

}

}

Class Derive: Public Base, Public Baseex

{

PUBLIC:

Void Qi (void ** P)

{

* p = (base *).

}

}

Int main (int Argc, char * argv [])

{

Derive Obj;

Baseex * p;

Obj.qi ((void **) & p);

P-> Print ();

Return 0;

}

You can run the code yourself and see the actual output results:

Base :: Output

As you can see, I am calling the Print function of the BaseEx class pointer, but from the output result, it actually calls the output function of the Base class! How is it doing? So I think You should see the mystery from the implementation of the QI function of the Derive class? Why can it do this clever transfer? This is due to the decision of C virtual function call (about this, you will see my article " The anti-assessment analysis of the C virtual function call "One question). This skill is used in the process of combing aggregation to ensure that the QueryinterFace method is correct!

转载请注明原文地址:https://www.9cbs.com/read-122260.html

New Post(0)