C++ Builder 參考手冊(cè) ? System::Sysutils ? TProc
用于定義兼容 Delphi 匿名函數(shù)的接口類(lèi)或 lambda 表達(dá)式
- 簡(jiǎn)介
- 成員
? 方法 - 例子
一. System::Sysutils::TProc... 簡(jiǎn)介
- 繼承關(guān)系:
IUnknown
?╙ System::IInterface
???┣ System::Sysutils::TProc
???┣ System::Sysutils::TProc__1
???┣ System::Sysutils::TProc__2
???┣ System::Sysutils::TProc__3
???┣ System::Sysutils::TProc__4
???┣ System::Sysutils::TFunc__1
???┣ System::Sysutils::TFunc__2
???┣ System::Sysutils::TFunc__3
???┣ System::Sysutils::TFunc__4
???┣ System::Sysutils::TFunc__5
???┗ System::Sysutils::TPredicate__1
- 頭文件:
#include <System.SysUtils.hpp> - 命名空間:
System::Sysutils - TProc, TProc__1, TProc__2, TProc__3, TProc__4,
TFunc__1, TFunc__2, TFunc__3, TFunc__4, TFunc__5,
TPredicate__1 用于定義兼容 Delphi 匿名函數(shù)的接口類(lèi)或 lambda 表達(dá)式:
_di_TProc, _di_TProc__1, _di_TProc__2, _di_TProc__3, _di_TProc__4,
_di_TFunc__1, _di_TFunc__2, _di_TFunc__3, _di_TFunc__4, _di_TFunc__5,
_di_TPredicate__1
二. System::Sysutils::TProc... 成員
__interface TProc : public System::IInterface
{
virtual void __fastcall Invoke() = 0 ;
};
template<typename T> __interface TProc__1 : public System::IInterface
{
virtual void __fastcall Invoke(T Arg1) = 0 ;
};
template<typename T1, typename T2> __interface TProc__2 : public System::IInterface
{
virtual void __fastcall Invoke(T1 Arg1, T2 Arg2) = 0 ;
};
template<typename T1, typename T2, typename T3> __interface TProc__3 : public System::IInterface
{
virtual void __fastcall Invoke(T1 Arg1, T2 Arg2, T3 Arg3) = 0 ;
};
template<typename T1, typename T2, typename T3, typename T4> __interface TProc__4 : public System::IInterface
{
virtual void __fastcall Invoke(T1 Arg1, T2 Arg2, T3 Arg3, T4 Arg4) = 0 ;
};
template<typename TResult> __interface TFunc__1 : public System::IInterface
{
virtual TResult __fastcall Invoke() = 0 ;
};
template<typename T, typename TResult> __interface TFunc__2 : public System::IInterface
{
virtual TResult __fastcall Invoke(T Arg1) = 0 ;
};
template<typename T1, typename T2, typename TResult> __interface TFunc__3 : public System::IInterface
{
virtual TResult __fastcall Invoke(T1 Arg1, T2 Arg2) = 0 ;
};
template<typename T1, typename T2, typename T3, typename TResult> __interface TFunc__4 : public System::IInterface
{
virtual TResult __fastcall Invoke(T1 Arg1, T2 Arg2, T3 Arg3) = 0 ;
};
template<typename T1, typename T2, typename T3, typename T4, typename TResult> __interface TFunc__5 : public System::IInterface
{
virtual TResult __fastcall Invoke(T1 Arg1, T2 Arg2, T3 Arg3, T4 Arg4) = 0 ;
};
template<typename T> __interface TPredicate__1 : public System::IInterface
{
virtual bool __fastcall Invoke(T Arg1) = 0 ;
};
1. System::Sysutils::TProc... 方法
| 方法 | 說(shuō)明 |
|---|---|
| public: | ? |
| Invoke | 匿名函數(shù)的實(shí)現(xiàn),純虛函數(shù)。子類(lèi)必須重載這個(gè)函數(shù)來(lái)實(shí)現(xiàn)匿名函數(shù)的功能 |
| IInterface:: | 從 System::IInterface 繼承過(guò)來(lái)的 |
| public: | ? |
| Supports | 判斷是否支持某個(gè)接口,如果支持,返回這個(gè)接口指針 |
| IUnknown:: | 從 IUnknown 繼承過(guò)來(lái)的 |
| public: | ? |
| QueryInterface | 判斷是否支持某個(gè)接口,如果支持,返回這個(gè)接口指針 |
| AddRef | 增加引用計(jì)數(shù) |
| Release | 減少引用計(jì)數(shù),如果引用計(jì)數(shù)等于 0 接口被銷(xiāo)毀,占用的資源被釋放 |
例1:有個(gè)浮點(diǎn)數(shù)數(shù)組,分別用從小到大和從大到小的順序輸出這個(gè)數(shù)組里面的數(shù)據(jù),使用 lambda 表達(dá)式
void TForm1::MyFunc(double *pArray, int iCount, _di_TFunc__3<double, double, int> pLambda)
{
for(int iMin=0; iMin<iCount-1; iMin++)
{
for(int iFind=iMin+1; iFind<iCount; iFind++)
{
if(pLambda->Invoke(pArray[iMin], pArray[iFind]) > 0)
{
double tmp = pArray[iMin];
pArray[iMin] = pArray[iFind];
pArray[iFind] = tmp;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double a[8] = { 1.23, 0.69, 3.14, 2.72, 0.02, 9.65, 7.34, 3.33 };
MyFunc(a, 8, [&](double a, double b)->int { return a > b; });
Memo1->Lines->Add(L"從小到大:");
for(int i=0; i<8; i++)
Memo1->Lines->Add(a[i]);
MyFunc(a, 8, [&](double a, double b)->int { return a < b; });
Memo1->Lines->Add(L"從大到?。?);
for(int i=0; i<8; i++)
Memo1->Lines->Add(a[i]);
}
運(yùn)行結(jié)果:

例2:依然是前面例1的 MyFunc 函數(shù),不用 lambda 表達(dá)式,采用繼承 System::TCppInterfacedObject<TFunc__3<double, double , int>> 的方法調(diào)用這個(gè)函數(shù)
void TForm1::MyFunc(double *pArray, int iCount, _di_TFunc__3<double, double, int> pLambda)
{
for(int iMin=0; iMin<iCount-1; iMin++)
{
for(int iFind=iMin+1; iFind<iCount; iFind++)
{
if(pLambda->Invoke(pArray[iMin], pArray[iFind]) > 0)
{
double tmp = pArray[iMin];
pArray[iMin] = pArray[iFind];
pArray[iFind] = tmp;
}
}
}
}
class TMyProcAsc : public TCppInterfacedObject<TFunc__3<double, double , int>>
{
public:
int __fastcall Invoke(double a, double b) // 這里是調(diào)用 lambda 執(zhí)行的內(nèi)容
{
return a > b;
}
};
class TMyProcDesc : public TCppInterfacedObject<TFunc__3<double, double , int>>
{
public:
int __fastcall Invoke(double a, double b) // 這里是調(diào)用 lambda 執(zhí)行的內(nèi)容
{
return a < b;
}
};
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double a[8] = { 1.23, 0.69, 3.14, 2.72, 0.02, 9.65, 7.34, 3.33 };
MyFunc(a, 8, new TMyProcAsc); // 用 TMyProcAsc 對(duì)象代替 lambda,自動(dòng)銷(xiāo)毀
Memo1->Lines->Add(L"從小到大:");
for(int i=0; i<8; i++)
Memo1->Lines->Add(a[i]);
MyFunc(a, 8, new TMyProcDesc); // 用 TMyProcDesc 對(duì)象代替 lambda,自動(dòng)銷(xiāo)毀
Memo1->Lines->Add(L"從大到小:");
for(int i=0; i<8; i++)
Memo1->Lines->Add(a[i]);
}
相關(guān):
- System::Sysutils::_di_TFunc__1
- System::Sysutils::_di_TFunc__2
- System::Sysutils::_di_TFunc__3
- System::Sysutils::_di_TFunc__4
- System::Sysutils::_di_TFunc__5
- System::Sysutils::_di_TPredicate__1
- System::Sysutils::_di_TProc
- System::Sysutils::_di_TProc__1
- System::Sysutils::_di_TProc__2
- System::Sysutils::_di_TProc__3
- System::Sysutils::_di_TProc__4
- System::Sysutils
- System::Classes::TThread::CreateAnonymousThread
- System::Classes::TThread
- System::Classes
- System::TCppInterfacedObject
- System::DelphiInterface
- System
C++ Builder 參考手冊(cè) ? System::Sysutils ? TProc