読者です 読者をやめる 読者になる 読者になる

Haruyuki Mohri

毛利春幸のブログです。

Use the "TAggregatedObject(IInterface)" in "C++Builder"

"TAggregatedObject" because it is for Delphi,

Use the "TCppAggregatedObject" in the case of "C++Builder".

https://gist.github.com/mojeld/82cba42c0679ddbefc89


How to use in Delphi, please look at the blog of Hosokawa san(Delphi MVP).

For "TCppInterfacedObject", please look here.


It makes a "IInterface" inherited interface.

__interface __declspec(uuid("{37A1B491-915F-4FE5-A2D8-2C319338BF6E}")) IFoo : public IInterface
{
    virtual int __fastcall setBar(int value) = 0;
    virtual int __fastcall getBar() = 0;
};

Create a class that inherits from "TCppAggregatedObject "

"IFoo" is interface of the above.

struct TFoo : public TCppAggregatedObject<IFoo >
{
    int FiBar;
    __fastcall TFoo(const _di_IInterface ctrl) : TCppAggregatedObject<IFoo>(ctrl)
        { FiBar = 0; }
    int __fastcall setBar(int value){ FiBar = value; return FiBar; };
    int __fastcall getBar(){return FiBar;};
    __property int Bar = {read=getBar,  default=0};
};

Run

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    _di_IInterface ifobj;
    IFoo* ifoo1 = new TFoo(ifobj);
    ifoo1->setBar(123);

    TFoo* foo1 = new TFoo(ifobj);
    foo1->setBar(234);

    IFoo* foo2 = ifoo1;
// delete ifoo1;//So interface, this can not delete.

    IFoo* foo3 = foo1;

    delete foo1;

    assert( foo2->getBar() ==123);
    assert( foo3->getBar() ==234);
}
//---------------------------------------------------------------------------

Reference

Implementing Interfaces: Delphi and C++ - RAD Studio
Inheritance and Interfaces - RAD Studio
Создание интерфейсов - C++ Builder - CyberForum.ru