1 c++ 类的数据成员的初始化发生在构造函数前

class InitialData
{
public:
int data1;
int data2; InitialData(int a, int b)
{
data1 = a; //this is assignment
data2 = b; //this is assignment
} /*
InitialData(int a, int b):data1(a),data2(b) //this is initial
{}
*/
}

2 不同cpp文件中的static变量初始化顺序不确定,千万不要互相引用

比如有个 FileSystem类

class FileSystem
{
public:
...
int getDiskNum() const;
}
extern FileSystem tfs;//in the hfile, used in the future for some other class

然后在一个叫global.cpp文件中申明了一个全局 FileSystem tfs;

然后有个Directory类用到tfs

#include "FileSystem.h"

class Directory
{
public:
Directory()
{
int t=tfs.numDisks();
}
};

有个人需要用Directory类,于是他申明

Directory tmpDir;这时就出现问题,tmpDir初始化时需要用到tfs,单编译器不能保证在编译tmpDir之前编译tfs,于是就会出现编译错误。

解决方案是采用singleton模式,利用函数得到全局变量

FileSystem& getFileSystem
{
static FileSystem tfs;
return tfs;
}

在Directory要调用tfs时用这个函数

#include "FileSystem.h"

class Directory
{
public:
Directory()
{
int t=getFileSystem();
}
};

最新文章

  1. 一个小白App开发需要了解的基本技术
  2. C 最熟悉的陌生人 (纪念当年就读的梅州市江南高级中学)
  3. 值得 Web 开发人员学习的20个 jQuery 实例教程
  4. Exception异常规范
  5. Android-BaiduMapSDK示例的key验证失败问题
  6. [转载] C++11中的右值引用
  7. C# winform 自定义控件
  8. [未完成][Mooc]关于IO总结
  9. linux共享文件samba安装与java读取外部文件夹方法
  10. Application之图书馆
  11. C语言参数传递
  12. 封装 INI 文件读写函数
  13. 智能合约语言 Solidity 教程系列7 - 以太单位及时间单位
  14. 双十一LoanMarket压力测试报告
  15. NOIP2011提高组 选择客栈
  16. 洛谷P1605:迷宫(DFS)
  17. [Python] 07 - Statements --> Functions
  18. markdown错误和问题
  19. Python进行Android开发步骤
  20. x01.calc: 编程语言

热门文章

  1. uva-211-The Domino Effect
  2. 实现自己的http server - loop_in_codes - C++博客
  3. python语言学习1——初识python
  4. Vagrant - 百度百科
  5. delphi 发送消息控制滚动条
  6. AJAX基础知识点学�
  7. Knockout应用开发指南 第七章:Mapping插件
  8. Mysql 双向关联触发器
  9. 简单的java mail发送邮件实例
  10. wamp在win7下64位系统memcache/memcached安装教程