1.智能指针
GOOGLE说智能指针能不用则不用
scoped_ptr扩展阅读,看起来很那啥的东西啊,但估计临场实用很难。
http://www.cnblogs.com/TianFang/archive/2008/09/15/1291050.html
2.引用参数
函数形参中,所有引用必须是const
3.函数重载
还是另外起个名字比较好
4.缺省参数,变长数组
禁用
5.友元
合理使用
友元使用例子
#include <iostream>
using namespace std;
class Screen
{
public:
Screen():x(0)
{
};
~Screen()
{
};
private:
int x;
friend class Window_Mgr;
};
class Window_Mgr
{
public:
Window_Mgr()
{
};
~Window_Mgr()
{
};
void test(Screen& s)
{
cout << s.x << endl;
};
};
int main()
{
Screen screen;
Window_Mgr mgr;
mgr.test(screen);
return 1;
}
6.异常
之前讲过,构造函数失败的处理,包括string类是如何实现构造函数的,
即当构造失败时只有异常可以处理,GOOGLE表示不用异常。
7.C++风格的类型转换
#include <stdio.h>
class MyClass
{
public:
MyClass();
~MyClass();
virtual void test();
void test2();
private:
};
MyClass::MyClass()
{
}
MyClass::~MyClass()
{
}
void MyClass::test()
{
printf("Myclass\n");
}
void MyClass::test2()
{
printf("Myclass test\n");
}
class Myclass2:public MyClass
{
public:
Myclass2();
~Myclass2();
void test();
void test2();
private:
};
Myclass2::Myclass2()
{
}
Myclass2::~Myclass2()
{
}
void Myclass2::test()
{
printf("Myclass2\n");
}
void Myclass2::test2()
{
printf("Myclass test2\n");
}
int main()
{
int x = static_cast<int>(3.6);//强制转换
printf("%d\n",x);
const char* a = "123";
char* b = const_cast<char*>(a);//移除const属性
int c = reinterpret_cast<int>(a);//类型转换
printf("%d\n",c);//a的地址/指针的值
MyClass* test = new Myclass2;//基类必须实现多态
test->test();
test->test2();//隐藏
Myclass2* test2 = dynamic_cast<Myclass2*>(test);//确定类型信息
test2->test();
test2->test2();
return 1;
}
8.自增自减和const
尽量使用前置自增自减,尽量使用const
9.整形
<stdint.h>拥有精确宽度类型
考虑到
for (unsigned int i = foo.Length()-1; i >= 0; --i) ...
不要使用无符号型表示非负数
10.关于64位下的可移植性
C99标准略过
结构体对齐示例
#include <stdio.h>
#include <stdint.h>
typedef struct
{
int64_t a;
char b;
}test;
int main()
{
//指针大小 WINDOWS中32位4字节 64位8字节
printf("%d\n",sizeof(intptr_t));
//WINDOWS中32 64位都是16字节
printf("%d\n",sizeof(test));
return 1;
}
关于结构体对齐扩展阅读见
http://blog.csdn.net/zlhy_/article/details/8296838
李菊福
11.预处理宏
VS2012查看预处理过的文件
项目属性->C/C++->预处理器->预处理到文件/保留注释
12.整数用0 实数用0.0 指针用NULL 字符创用'\0'
13.用sizeof(var)代替sizeof(type)