包含标签 c++11 的文章

左值和右值

本文整理自网文,如有冒犯,请告知删除。 左值(lvalue)和右值(rvalue) 左值(lvalue)和右值(rvalue)是c/c++中的基础概念。 简单理解为:有名字的是左值,没有名字的是右值。 C C语言中,左、右值均可为变量或表达式 C语言中,左值可出现在=号左边,也可出现在=号右边 C语言中,右值只能出现在=号右边 int x, y; x = 1; y = 2; x = y; y = x; 2 = x; //err x,y为左值,2为右值 CPP CPP语言中,一个表达式会产生一个左值,或者一个右值,前者称为左值表达式,后者称为右值表达式。 CPP语言中,对于基本类型,左值和右值概念与C语言相同。 CPP语言中,对于自定义类型,右值允许通过它的成员函数进行修改。 class A { public: A(int i) : _i(i) {} A& operator=(const A& other) { _i = other._i; return *this; } void set(int i) { _i = i; } int get() { return _i; } private: int _i; }; int main() { A a(5); auto f = [&] () ->A { return a; } f().……

阅读全文

lambda函数

格式 [捕捉列表] (参数) mutable -> 返回值类型 {函数体} lambda 表达式是一种匿名函数,即没有函数名的函数;该匿名函数是由数学中的λ演算而来的。 种类 []{} 最简单的lambda函数 [var] 表示以值传递方式捕捉变量var int main() { int x = 5; auto f = [x]{ //x is read-only int y; y = x + 1; std::cout << y << std::endl; }; f(); return 0; } $>> 6 [=] 表示值传递捕捉所有父作用域变量 int main() { int x = 5; int y = 6; auto f = [=]{ //x,y is read-only int z; z = x + y; std::cout << z << std::endl; }; f(); return 0; } $>> 11 [&var] 表示以引用传递方式捕捉变量var int main() { int x = 5; auto f = [&x]{ x++; }; f(); std::cout << x << std::endl; return 0; } $>> 6 [&] 表示引用传递捕捉所有父作用域变量 int main() { int x = 5; int y = 0; auto f = [&]{ x++; y++; }; f(); std::cout << x << std::endl; std::cout << y << std::endl; return 0; } $>> 6 $>> 1 [this] 表示值传递方式捕捉当前的this指针 class A { public: void foo() { auto f = [this] { bar(); }; f(); } void bar() { std::cout << "bar" << std::endl; } }; int main() { A a; a.……

阅读全文

c++11的weak_ptr

why? class A { public: A() { std::cout << "A ctor" << std::endl;} ~A() { std::cout << "A dtor" << std::endl; } std::shared_ptr<B> spB; }; class B { public: B() { std::cout << "B ctor" << std::endl;} ~B() { std::cout << "B dtor" << std::endl; } std::shared_ptr<A> spA; }; int main() { std::shared_ptr<A> spA = std::make_shared<A>(); std::shared_ptr<B> spB = std::make_shared<B>(); spA->spB_ = spB; spB->spA_ = spA; return 0; } output: A ctor B ctor 从以上结果可以看出,A、B均未释放,造成内存泄漏。 根本原因在于,A、B之间的循环引用。 how? weak_ptr用来与shared_ptr共同使用,避免内存泄漏。 class A { public: A() { std::cout << "A ctor" << std::endl;} ~A() { std::cout << "A dtor" << std::endl; } void foo() { std::cout << "I am foo" << std::endl; } std::weak_ptr<B> wpB_; }; class B { public: B() { std::cout << "B ctor" << std::endl;} ~B() { std::cout << "B dtor" << std::endl; } std::weak_ptr<A> wpA_; }; int main() { std::shared_ptr<A> spA = std::make_shared<A>(); std::shared_ptr<B> spB = std::make_shared<B>(); spA->wpB_ = spB; spB->wpA_ = spA; spB->wpA_.……

阅读全文

c++11的auto关键字

c++11之前 c++11之前,auto关键字用来表示变量的存储期 类型推断 变量声明时可以用auto,编译器会自动推断数据类型 auto i = 10; std::vector<int> v; for (auto it = v.begin(); it != v.end(); it++) { std::cout << *v << std::endl; } 可见,auto使代码更简洁,增强了可读性。……

阅读全文

c++11的shared_ptr

what? 智能指针是基于RAII思想的c++类(类模板) why? 智能指针帮助c++使用者更好的管理内存 how? 使用方法 eg: std::shared_ptr<int> foo = std::make_shared<int> (10); // same as: //std::shared_ptr<int> foo (new int(10)); std::cout << "*foo: " << *foo << std::endl; eg: class A { public: A(int i) : m_i(i) { } void foo() { std::cout << m_i << std::endl; } private: int m_i; }; typedef std::shared_ptr<A> A_PTR; A_PTR aPtr = std::make_shared<A>(10); std::cout << aPtr->foo() << std::endl; ……

阅读全文