C++Builder中__closure关键字的用法
来源:互联网 发布:tk的域名 编辑:程序博客网 时间:2024/05/21 10:21
最近在用C++Builder时,想要将对象1的成员函数指针赋给对象2的成员变量,然后在对象2内通过该成员变量访问对象1的成员函数。貌似标准的C++不支持这样的操作,但是C++Builder提供了__closure关键字,感觉还挺好用的,测试代码如下:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Unit1.h
//---------------------------------------------------------------------------
class
Test1 {
private
:
int
m_Add;
public
:
__fastcall Test1(
int
Add) {
m_Add = Add;
};
virtual
__fastcall ~Test1() {};
int
__fastcall TestFunction1(
int
param1);
void
__fastcall Test11();
};
//---------------------------------------------------------------------------
class
Test2 {
public
:
__fastcall Test2() {};
virtual
__fastcall ~Test2() {};
int
__fastcall(__closure *TestFunction2)(
int
param1);
void
__fastcall Test22();
};
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
// Unit1.cpp
//---------------------------------------------------------------------------
int
__fastcall Test1::TestFunction1(
int
param1) {
return
param1 + m_Add;
}
//---------------------------------------------------------------------------
void
__fastcall Test1::Test11() {
Test2 *test2 =
new
Test2();
test2->TestFunction2 = &TestFunction1;
test2->Test22();
delete
test2;
};
//---------------------------------------------------------------------------
void
__fastcall Test2::Test22() {
printf
(
"Result: %d"
, TestFunction2(1));
}
01
02
03
04
05
06
07
08
09
// main.cpp
//---------------------------------------------------------------------------
int
_tmain(
int
argc, _TCHAR* argv[]) {
Test1 *test =
new
Test1(1);
test->Test11();
delete
test;
getchar
();
return
0;
}
0 0
- C++Builder中__closure关键字的用法
- C++Builder中MessageBox的基本用法
- C++Builder中MessageBox的基本用法
- C++Builder中MessageBox的基本用法
- C/C++中extern关键字的用法
- C/C++中static关键字的用法
- C语言中 extern 关键字的用法
- c中volatile关键字的用法
- C语言 中volatile关键字的用法
- C/C++中extern关键字的用法
- c语言中关键字的用法
- C语言中const关键字的用法
- C/C++中extern关键字的用法
- C语言中const关键字的用法
- C语言中const关键字的用法
- C语言中const关键字的用法
- C语言中const关键字的用法
- C++Builder为C++增加的关键字
- iOS 网络资源汇总之UI
- BroadcastReceiver源码解析(二)
- JAVA代码发送邮件示例和解释
- linux下oracle11.2.0.1升级到11.2.0.4
- Ubuntu15.04 运行caffe+mnist的demo
- C++Builder中__closure关键字的用法
- Java 读取网页简易程序
- 数据保护方案的演进
- HTTP协议详解
- 论马尔可夫链的稳定状态与MFC结合在天气预测中的应用
- java 对象序列化
- java解析xml文件(三种方式-(dom ,jdom ,dom4j)
- 【模版】最小表示法
- CSS中zoom:1的作用 ,小标签大作用