forward declaration 前置声明
2015-06-30 11:19
423 查看
when class A and class relationship is very close, like that
the two .h file will contain each other, to compile the project, it will be loop
the right way
use forward declaration, you only can declaration a class reference or a point to a class ,
becase it’s size is knowed by compiler, the compile can compute the size of the class.
declaration a class object ,will be error
// chair.h #include "room.h" class chair { room* a; public: chair(void); ~chair(void); }; // chair.cpp #include <iostream> //#include "room.h" #include "chair.h" chair::chair() //: a ( *(new chair() ) ) { printf("%d Line\n",__LINE__); //a =new chair ; //terrible, will be loop printf("here is chair\n"); std::cout<<"chair's constructor\n"; } chair::~chair() { std::cout<<"chair's destructor\n"; } // room.h #include "chair.h" class chair; class room { chair* b; public: room(void); virtual ~room(void); }; //room.cpp #include <iostream> #include "room.h" //#include "chair.h" room::room() { printf("%d Line\n",__LINE__); b = new chair; std::cout<<"room's constructor\n"; } room::~room() { std::cout<<"room's destructor\n"; } ////main.cpp #include <stdio.h> #include "room.h" #include "chair.h" int main() { setbuf(stdout,NULL); printf("%d Line\n",__LINE__); room a; printf("%d Line\n",__LINE__); chair b; printf("%d Line\n",__LINE__); }
the two .h file will contain each other, to compile the project, it will be loop
the right way
// chair.h class room; class chair { room* a; public: chair(void); ~chair(void); }; // chair.cpp #include "room.h" #include "chair.h" chair::chair() //: a ( *(new chair() ) ) { printf("%d Line\n",__LINE__); //a =new chair ; //terrible, will be loop printf("here is chair\n"); std::cout<<"chair's constructor\n"; } chair::~chair() { std::cout<<"chair's destructor\n"; } // room.h class chair; class room { chair* b; public: room(void); virtual ~room(void); }; //room.cpp #include <iostream> #include "room.h" #include "chair.h" room::room() { printf("%d Line\n",__LINE__); b = new chair; std::cout<<"room's constructor\n"; } room::~room() { std::cout<<"room's destructor\n"; } //main.cpp #include <stdio.h> #include "room.h" #include "chair.h" int main() { setbuf(stdout,NULL); printf("%d Line\n",__LINE__); room a; printf("%d Line\n",__LINE__); chair b; printf("%d Line\n",__LINE__); }
use forward declaration, you only can declaration a class reference or a point to a class ,
becase it’s size is knowed by compiler, the compile can compute the size of the class.
declaration a class object ,will be error
相关文章推荐
- 黑马程序员--java基础学习笔记1
- 练习:多线程实现后台播放背景音乐的service
- 关于如何在activity之外调用Conetxt
- iOS-UI控件之UITableView(二)- 自定义不等高的cell
- dede操作成功信息提示修改
- mysql 中 unix_timestamp和from_unixtime 时间戳函数
- sizeof和strlen区别与不同
- ps、grep和kill联合使用杀掉进程
- 使用testNG进行并发性能测试
- Android高效显示图片详解(一)
- matlab中各种距离计算方式
- NodeJs学习笔记一(ubuntu下环境搭建)
- 架构师速成5.2-如何掌握综合性技能
- Android Studio 视图解析
- 架构师速成5.2-如何掌握综合性技能 分类: 架构师速成 2015-06-30 11:18 405人阅读 评论(0) 收藏
- 苹果浏览器Safari对JS函数库中newDate()函数中的参数的解析中不支持形如“2020-01-01”形式
- angularJS开发碰到的问题
- [置顶] 微信APP支付
- linux模拟按键
- 网狐框架分析六--整体框架 20150623