摘要: 以书上的代码为例processWidget(shared_ptr(new Widget), priority())虽然使用了智能指针来管理资源但是,由于参数值计算顺序的不确定性,new智能并不一定立刻在priority之前初始化了智能指针,这样如果priority抛出异常,资源就泄露了。。。 阅读全文
posted @ 2014-12-21 22:03 卖程序的小歪 阅读(158) 评论(0) 推荐(0) 编辑
摘要: 15. 智能指针可以通过get操作#include #include #include using namespace std;class Orange {private: int weight;public: Orange(int w) : weight(w) {} int ge... 阅读全文
posted @ 2014-12-21 21:47 卖程序的小歪 阅读(140) 评论(0) 推荐(0) 编辑
摘要: #include #include #include using namespace std;class Kiwi {private: int weight;public: Kiwi(int w) : weight(w) {} ~Kiwi() { cout p(new... 阅读全文
posted @ 2014-12-21 21:08 卖程序的小歪 阅读(312) 评论(0) 推荐(0) 编辑
摘要: #include #include #include using namespace std;class Kiwi {private: int weight;public: Kiwi(int w) : weight(w) {} ~Kiwi() { cout p(new... 阅读全文
posted @ 2014-12-21 20:33 卖程序的小歪 阅读(154) 评论(0) 推荐(0) 编辑
摘要: 当我们自己编写拷贝构造函数时,编译器就不会为该类生成默认拷贝构造函数了,对于assignment operator也是如此。1. 拷贝构造函数中记得调用父类的拷贝构造函数,或者相应复制过程class Man {private: int age;public: Man(int _age =... 阅读全文
posted @ 2014-12-21 20:07 卖程序的小歪 阅读(169) 评论(0) 推荐(0) 编辑
摘要: 1. 返回一个reference to *this返回一个指向自身的引用符合惯例,可以进行如(a=c).modify()类似的操作,即可以形成链式操作,否则修改的只是一个临时对象。这个和Java中常用的builder模式是一个道理2. 自我赋值的检测和异常安全赋值进行前进行自我检测,相同就直接返回。... 阅读全文
posted @ 2014-12-21 19:30 卖程序的小歪 阅读(229) 评论(0) 推荐(0) 编辑
摘要: 看过C++对象模型的话就可以知道,在构造基类时,完整的vtable没有建立起来(表项没有被相应的子类函数替换),因而无法调用到子类的函数(即构造函数中的virtual函数是本类里的方法,不是virtual的)。书中也说即使调用了,因为构造函数的调用顺序,父类在构造时子类的成员还没有初始化可能,此时调... 阅读全文
posted @ 2014-12-21 19:00 卖程序的小歪 阅读(144) 评论(0) 推荐(0) 编辑
摘要: 异常不怎么用,C++能自己控制析构过程,也就有这个要求了。容器不能完全析构其中的元素真是太危险了 阅读全文
posted @ 2014-12-21 16:48 卖程序的小歪 阅读(115) 评论(0) 推荐(0) 编辑
摘要: 主要讲了,1. virtual析构函数的作用与调用顺序2. 使用时机,并不是使用了继承就要把基类的析构函数变为虚函数(virtual),只有当用于多态目的时才进行一个virtual析构函数的定义。3. 不要继承那些没有将析构函数定义为virtual的类来实现多态行为对于1:有些类继承可能只是一个组合... 阅读全文
posted @ 2014-12-21 16:21 卖程序的小歪 阅读(136) 评论(0) 推荐(0) 编辑
摘要: 这节讲了下如何防止对象拷贝(隐藏并不能被其他人调用)两种方法:1. 将拷贝构造函数声明为private 并且声明函数但不进行定义#include #include class Dummy {public: Dummy(int d = 0) : data(d) {} Dummy* getC... 阅读全文
posted @ 2014-12-21 15:52 卖程序的小歪 阅读(459) 评论(0) 推荐(0) 编辑
摘要: 主要讲了1. 一般情况下编译器会为类创建默认的构造函数,拷贝构造函数和copy assignment函数2. 执行默认的拷贝构造/copy assignment函数时,如果成员有自己的拷贝构造/copy assignment函数就执行它,否则就按bit拷贝/赋值3. 几种编译器不会为类生成默认的co... 阅读全文
posted @ 2014-12-21 15:23 卖程序的小歪 阅读(282) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: int maxProduct(int A[], int n) { if (A == NULL || n res) { res = tr; } if (tr ... 阅读全文
posted @ 2014-12-21 14:14 卖程序的小歪 阅读(117) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: string convertToTitle(int n) { if (n 0) { n--; res = (char)(n % 26 + 'A') + res; n ... 阅读全文
posted @ 2014-12-21 13:17 卖程序的小歪 阅读(92) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: int maximumGap(vector &num) { int len = num.size(); int gap = 0; sort(num.begin(), num.end()); ... 阅读全文
posted @ 2014-12-21 12:10 卖程序的小歪 阅读(216) 评论(0) 推荐(0) 编辑