c++碎片知识
- c vs c++
c++比c具有以下新功能:
1)变量转换(Functional Style, Dynamic, Static, Reinterpret, Const_cast)
2)Namespaces
3) new/delete
4) 重载、重写
5)很多面向对象的类
6)更多的标准库
7)异常处理
下面文档里有很多c++的介绍
http://blog.jobbole.com/82460/
RAII是Resource Acquisition Is Initialization的简称,是C++语言的一种管理资源、避免泄漏的惯用法。利用的就是C++构造的对象最终会被销毁的原则。RAII的做法是使用一个对象,在其构造时获取对应的资源,在对象生命期内控制对资源的访问,使之始终保持有效,最后在对象析构的时候,释放构造时获取的资源。
Lambda表达式:
基本语法:[capture](parameters) mutable ->return-type{statement}
捕捉列表有以下几种形式:
- [var]表示值传递方式捕捉变量var;
- [=]表示值传递方式捕捉所有父作用域的变量(包括this);
- [&var]表示引用传递捕捉变量var;
- [&]表示引用传递方式捕捉所有父作用域的变量(包括this);
- [this]表示值传递方式捕捉当前的this指针。
https://www.devbean.net/2012/05/cpp11-lambda/
http://www.jellythink.com/archives/668
单例的最优算法:
http://blog.yangyubo.com/2009/06/04/best-cpp-singleton-pattern/
class Singleton {
public:
static Singleton& Instance() {
static Singleton theSingleton;
return theSingleton;
}
/* more (non-static) functions here */
private:
Singleton(); // ctor hidden
Singleton(Singleton const&); // copy ctor hidden
Singleton& operator=(Singleton const&); // assign op. hidden
~Singleton(); // dtor hidden
};
2. 数组形参
void UpperCase( char str[] ) // 将 str 中的小写字母转换成大写字母
{
for( size_t i=0; i<sizeof(str)/sizeof(str[0]); ++i )
if( 'a'<=str[i] && str[i]<='z' )
str[i] -= ('a'-'A' );
}
char str[] = "aBcDe";
cout << "str字符长度为: " << sizeof(str)/sizeof(str[0]) << endl;
UpperCase( str );
cout << str << endl;
这段代码的问题是数组参数相当于指针,只能转化4个字节的大小写。
1) 如果数组边界的精确数值非常重要,可以如下定义
void average(int (&ary)[12])
更通用的模版写法如下:
Template <int n>
Void average(int (&ary)[n]);
2) 常规写法如下:
Void average_n(int ary[],int size);
3. 转型操作
const_cast<T*> 去除const
static_cast<T*> 基类转化为派生类
reinterpret_cast<T*> 按位转化
dynamic_cast<T*> 会运行检查,必须是一个指向带有虚函数的类类型的指针
4. 常量成员函数如果想修改非静态数据成员,可以声明为mutable
Class x{
Public:
//…
Int getValue() const {
If(!isComputed_) isComputed_=true;
return computedValue_;
}
private:
mutable bool isComputed_;
};
http://blog.csdn.net/u010889616/article/details/48630079
std::bind原理图解
http://www.cnblogs.com/xusd-null/p/3698969.html#3081606
浙公网安备 33010602011771号