摘要: 首先考虑一个工厂函数Investment * createInvestment();void f(){ Investment * pInv = createInvestment(); ... delete pInv;}至少上面这个函数是不安全的,例如如果...里面包含ret... 阅读全文
posted @ 2015-10-05 21:43 eversliver 阅读(187) 评论(0) 推荐(0) 编辑
摘要: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of 阅读全文
posted @ 2015-10-05 21:12 eversliver 阅读(4850) 评论(0) 推荐(0) 编辑
摘要: Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two nu... 阅读全文
posted @ 2015-10-05 17:56 eversliver 阅读(383) 评论(0) 推荐(0) 编辑
摘要: 首先要说的是当对一个已经定义了构造函数以及赋值运算符(拷贝)的class增加了成员的时候,一定要记得为这个class重新改写构造函数以及拷贝赋值运算符。 下一个容易遗忘的点在继承中,看下面的代码: 1 class Customer 2 { 3 public: 4 //.... 阅读全文
posted @ 2015-10-05 16:45 eversliver 阅读(284) 评论(0) 推荐(0) 编辑
摘要: 本来的版本是这样的:1 Widget & Widget::operator=(Widget rhs)2 {3 delete pb;//这里可能直接将rhs的pb删除了4 pb = new (*rhs.pb);5 return *this;6 }这里的代码完全无法处理自赋值的情... 阅读全文
posted @ 2015-10-05 15:15 eversliver 阅读(206) 评论(0) 推荐(0) 编辑
摘要: 例如对象x,y,z。要实现连锁赋值(假设operator=已经重载过了):x = y = z,那么operator=则必须返回一个*this。 注意这个条款不仅仅适合于operator=,对于operator+=与赋值相关的操作也是同样适合的。所以的想要连环赋值的情况都应该这样去设计,尽管编译... 阅读全文
posted @ 2015-10-05 14:56 eversliver 阅读(213) 评论(0) 推荐(0) 编辑
摘要: 在构造以及析构函数期间不要调用virtual函数,因为这类调用从不下降到derived class中。例如说下面这个例子: 1 class Transaction{ 2 public: 3 Transaction(); 4 virtual void logTransactions()... 阅读全文
posted @ 2015-10-05 12:26 eversliver 阅读(273) 评论(0) 推荐(0) 编辑