------------- c ++ ===============================
alignas 字节对齐,以最小单位的来对齐,
struct alignas(1) s {};
typeid(a).name() 查找变量类型
constexpr 定义在 函数前面,如果能直接求出来的值,会直接求出来在编译期间
constexpr int fact(int n) { return n < 1 ? 1 : (n*fact(n - 1)); }
struct A { double x}
decltype(a->x) y; 和auto 很像
decltype((a->x)) y; double& 有括号与无括号的区别是在于引用
template<typename T,typename U>
auto add (T a,U b) -> decltype(a + b){
return a + b;
}
add (1,2) 整数型
add (1.0,2) 双精度,1.0默认为双精度
enum class NewColor {Red,Green = 20,Blue};
NewClor r == NewColor::Blue;
int a = static_cast<int>(r);// 明确告诉编译器,这样子转换
static_assert 编译时才检查到的,才用
assert 运行时检查
static_cast 告诉编译器就需要这样子
thread_local 多线程用到
using a = map<int ,cstring>; 等同 typedef map<int ,cstring> a;
volatile 声明不用优化
class RuleOfThree {
public:
char * szBuf;
private:
RuleOfThree
}
右值引用
int a = 0
int& e = a ; 操作e,等同 a*
int&& e = 5; 但不能这样子改 int& e = 5;
auto d = std::move(b) ; 把左值转成右值
Base *P = new Derived();
Derived *pd1 = static_cast<Derived *>(P);
Derived *pd2 = dynamic_cast<Derived *>(P); // 派生类转基类没问题 ,但下面是基类转派生类,就会有问题 了,所以要用dynamic_cast的原因,转换失败时,会返回NULL
Base *P = new Base;
Derived *pd3 = static_cast<Derived *>(P);
Derived *pd4 = dynamic_cast<Derived *>(P);
类的 析构函数不能有异常,否则会有dump 因为编译器默认是不能有异常
struct Base {
virtual void f() {std::cout << "base\n" ;}
virtual ~Base() {}
};
struct Derived:Base {
void f() override {
-- 改写基类的f函数 用关键词 override
}
};
std::shared_ptr 智能指针
auto_ptr
shared_ptr
============================================================= weak_ptr ================================================
class Parent;
typedef std::shared_ptr<Parent> ParentPtr;
typedef std::weak_ptr<Parent> WeakParentPtr;
class chlid : public std::enable_shared_from_this<chlid>
// 定义岐义模板参数,原理是通过Weakptr.lock 然后构造出来this
{
public:
WeakParentPtr father;
chlid();
~chlid();
void checkRelation();
};
typedef std::shared_ptr<chlid> ChildPtr;
typedef std::weak_ptr<chlid> WeakChildPtr;
class Parent : public std::enable_shared_from_this<Parent>
{
public:
WeakChildPtr son;
Parent();
~Parent();
void checkRelation();
};
chlid::chlid() { std::cout << "In child\n"; }
Parent::Parent() { std::cout << "In fahter\n"; }
chlid::~chlid() { std::cout << "bye child\n"; }
Parent::~Parent() { std::cout << "bye fahter\n"; }
void handleChildandParent(const ParentPtr & p, const ChildPtr &c)
{
auto cp = c->father.lock();
auto pc = p->son.lock();
if (cp == p && pc == c)
{
std::cout << "right relation \n";
}
else
{
std::cout << "oop!!!!!!\n";
}
}
void chlid::checkRelation() {
auto ps = father.lock();
if (ps)
{
handleChildandParent(ps, shared_from_this());
}
}
void Parent::checkRelation() {
auto ps = son.lock();
if (ps)
{
handleChildandParent(shared_from_this(), ps);
}
}
========================================= unique_ptr ================================================
void trasnsfer(uniquePtr obj)
{
std::cout << obj>->id() << std::endl;
}
特性:引用唯一;
class Parent;
typedef std::unique_ptr<Object> uniquePtr;
uniquePtr Obj(new Object(1));
auto p = Obj.get()
if (p) { std::cout << p->id() std::endl; }
p = Obj.release();
delete p;
Obj.reset();// 释放指针?
Obj.reset(new Object(2));
trasnsfer(std::move(Obj)); // 如果给别人引用了,那么,他的值应是nullptr;