
effective c++
WorstCoder
能够做出困难的算法题是最快乐的事情!
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
effective c++若不想使用编译器自动生成的函数,就应该明确拒绝
友元函数: 友元函数只是一个普通函数,并不是该类的类成员函数,它可以在任何地方调用,友元函数中通过对象名来访问该类的私有或保护成员。class A{public: A(int _a):a(_a){}; friend int getA_a(A &_classA);//友元函数private: int a;};int getA_a(A &_classA){原创 2015-09-09 20:42:12 · 527 阅读 · 0 评论 -
effective c++ 在资源管理类中小心copying 行为
资源取得时机便是初始化时机class Mutex{};void lock(Mutex* p){}void unlock(Mutex* p){}class Lock{public: explicit Lock(Mutex *p) :mutexPtr(p) { lock(mutexPtr); } ~Lock() {原创 2015-10-24 20:05:25 · 451 阅读 · 0 评论 -
effective c++以独立语句将newed对象植入智能指针
int priority(){}struct Widget{};void processWidget(shared_ptr<Widget>pw,int priority){}int main(){ processWidget(new Widget,priority()); //产生编译错误,因为shared_ptr构造函数需要一个原始指针 //但是该构造函数是一个ex原创 2015-10-27 15:12:45 · 572 阅读 · 1 评论 -
effective c++让接口容易被正确使用,不易被误用
class Month{public: static Month Jan(){return Month(1);} static Month Feb(){return Month(2);} //......private: explicit Month(int i):val(i){} int val;};class Year{public:原创 2015-10-28 21:48:19 · 511 阅读 · 0 评论 -
effective c++尽量少做转型动作(1)
为啥要分两次写呢,因为这篇实在是太长了,因为畏惧他的长度,所以这几天竟然一直拖着没有写effective c++的读书笔记,导致也没有往后面太看。。。所以,以后如果文章太长了,不要惧怕,分开写就好了,还是重在坚持看这章内容,一下子就被其中的const_cast(expression)表达式所吸引了,c++就是作死小能手,想怎么耍就怎么耍!它可以将对象的常量性转除!!然后马上查了一下c++ refer原创 2015-11-15 00:06:33 · 631 阅读 · 0 评论 -
effective c++:必须返回对象时,别妄想返回其reference
class Rational{public: Rational(int numerator = 0,int denominator = 1) { this -> n = numerator; this -> d = denominator; }private: friend const Rational operator*(con原创 2015-10-31 15:49:40 · 516 阅读 · 0 评论 -
effective c++避免返回handles指向对象内部成分(2)
class Point{public: Point(int _x,int _y):x(_x),y(_y){} void setX(int _x){x = _x;} void setY(int _y){y = _y;} int getX()const{return x;} int getY()const{return y;}private: int原创 2015-11-21 15:03:03 · 778 阅读 · 0 评论 -
effective c++:宁以pass-by-reference-to-const替代pass-by-value
class People{public: People() { cout << " construct fun " << endl; } People(const People& i) :a(i.a) { cout << " copy construct " << endl; } People &op原创 2015-10-31 00:42:13 · 637 阅读 · 0 评论 -
effective c++ 为多态基类声明virtual析构函数
C++的多态性class TextBlock{public: virtual void foo() { cout << "TextBlock" << endl; }};class Text : public TextBlock{public: void foo() { cout << "Text" << endl;原创 2015-09-10 22:39:59 · 402 阅读 · 0 评论 -
effective c++ 将成员变量声明为private
一旦你将一个成员变量声明为public或protected而用户开始使用它,就很难改变那个成员变量所涉及的一切。如果改变:太多代码需要重写,重新测试,重新编写文档,重新编译#include<iostream>using namespace std;//////////////////////////////////////////////////////////////////////////原创 2015-11-02 13:07:51 · 616 阅读 · 0 评论 -
effective c++ 考虑写出一个不抛出异常的swap函数
#include<algorithm>#include<iostream>#include<cstdio>#include<vector>class WidgetImpl{ //...... std::vector<int>v;};class Widget{public: void swap(Widget& other){ //原书给的是原创 2015-11-06 22:49:20 · 640 阅读 · 0 评论 -
effective c++ 避免返回handles指向对象内部成分(1)
class Point{public: Point(int _x,int _y):x(_x),y(_y){} void setX(int _x){x = _x;} void setY(int _y){y = _y;}private: int x,y;};class RectData{public: RectData(Point _ulhc,Poi原创 2015-11-20 21:10:20 · 588 阅读 · 0 评论 -
effective c++ 尽量少做转型动作(2)
C风格的转型动作 (T)expression //将expression转型为T 函数风格的转型动作 T(expression)//将expression转型为T旧式转型依旧合法,但是新式转型较受欢迎 1.容易在代码中识别出来,无论是人眼还是grep等工具 2.各转型目标愈窄化,编译器愈可能诊断出错误的运用,比如只有const_cast能去掉常量性class Base{public:原创 2015-11-19 17:22:32 · 598 阅读 · 0 评论 -
effective c++成对使用new 和 delete时要采取相同形式
string *str1 = new string;delete str1;//删除一个对象string *str2 = new string[100];delete []str2;//删除一个由对象组成的数组如果使用delete[] str1或者delete str2都是未定义的 数组所用的指针通常还包括数组大小的记录,以便delete知道需要调用多少次析构函数 编译器往往会这么实现对象原创 2015-10-26 11:16:35 · 510 阅读 · 0 评论 -
effective c++尽可能延后变量定义式的出现时间
原书用的是一个c++标准库的函数,这里为了简练,就随便写了一个函数,但意思相近void doSth(int i){ string s; //过早的定义了这个变量 //如果i == 0则导致白白的声明了这个变量 if(i == 0)throw logic_error("i == 0"); s = "string";}可以改成这样void doSth(int原创 2015-11-09 23:08:29 · 579 阅读 · 0 评论 -
effective c++在资源管理类中提供对原始资源的访问
有关访问资源管理类中的原生指针#include <iostream>#include <cstdio>using namespace std;struct FontHandle{};FontHandle getFont(){}void releaseFont(FontHandle f){}class Font//资源管理类{public: explicit Font(Font原创 2015-10-25 23:27:03 · 706 阅读 · 0 评论 -
effective c++:了解c++默默编写并调用了哪些函数
编译器可以暗自为class创建default构造函数,copy构造函数,copy assignment操作符,以及析构函数#include <iostream>#include<cstdio>#include<cstring>#include<effective_c.h>using namespace std;template<class T>class TextBlock{publ原创 2015-09-08 23:10:10 · 460 阅读 · 0 评论 -
effective c++导读
声明为explicit的构造函数更受欢迎,它禁止编译器执行非预期的类型转换#include <iostream>#include<cstdio>#include<cstring>using namespace std;class B{public: explicit B(int a = 1){}};class C{public: C(int a = 1){}};原创 2015-09-04 22:42:44 · 428 阅读 · 0 评论 -
effective c++ 尽可能的使用const
#include <iostream>#include<cstdio>#include<cstring>#include<effective_c.h>#include<set>#include<iterator>#include<vector>using namespace std;vector<int>vec;int main(){ const int * a;原创 2015-09-06 10:31:53 · 391 阅读 · 0 评论 -
effective c++ 尽量以const enum inline 替换 #define
为了将常量的作用域限制在class内,你必须让它成为class的一个成员#include <iostream>#include<cstdio>#include<cstring>#include<effective_c.h>using namespace std;class A{public: A(); const static int NUM = 1;//声明而非定义原创 2015-09-05 15:18:17 · 478 阅读 · 0 评论 -
effective c++ 确定对象被使用前已经被初始化
class TextBlock{public: TextBlock(const string & name,const string & address) { cnt = 0; theName = name; theAddress = address; //赋值而非初始化 }private: int原创 2015-09-07 12:06:16 · 369 阅读 · 0 评论 -
在operator=中处理自我赋值
自我赋值发生在Widget a;//.......a = a;上面这种情况一般不会发生。。 但是如果b[i] = b[j];*p1 = *p2i == j 或者 p1和p2指向同一个对象 自我赋值就发生了class Widget{public: Widget &operator = (const Widget& c) { delete pb;原创 2015-09-29 21:23:08 · 437 阅读 · 0 评论 -
effective c++绝不在构造和析构过程中调用virtual函数
不应该在构造函数和析构函数期间调用virtual函数class Transaction{public: Transaction() { init(); } void init() { logTransaction(); } virtual void logTransaction() = 0;};class原创 2015-09-29 00:12:15 · 701 阅读 · 3 评论 -
effective c++ 令operator=返回一个reference to *this
令operator=返回一个reference to *thisclass Widget{public: Widget & operator = (const Widget& c) { return *this; //这个协议也适用于+= -= *= }};因为为了实现连锁赋值int a,b,c; a = b = c = 0;原创 2015-09-29 17:37:09 · 421 阅读 · 0 评论 -
effective c++ 复制对象时勿忘每一个成分
自己定义拷贝构造函数的时候,派生类要注意复制基类的成分!class Base{private: int baseData;};class Derived : public Base{public: Derived(const Derived& c) :Base(c), derivedData(c.derivedData){} Derived &原创 2015-09-30 20:08:47 · 355 阅读 · 0 评论 -
effective c++ 别让异常逃离析构函数
C++并不禁止析构函数吐出异常,但是它不鼓励这样做class Widget{public: ~Widget() { throw 20; };};vector<Widget>vec;当vec被销毁时,他有责任销毁其内含的所有Widget 如果vec内含10个Widget,在析构第一个元素期间,有个异常被抛出,其他9个Widget还是应该被销毁,否则他们原创 2015-09-14 22:28:22 · 474 阅读 · 0 评论 -
effective c++ 若所有参数皆需要类型转换,请为此采用non-member函数
令classes支持隐式转换通常是个糟糕的主意。但是如果发生在数值类型上,比如设计一个class用来表现有理数,允许整数隐式转换为有理数似乎很是合理class Rational{public: Rational(int numerator = 0,int denominator = 1){} int numerator()const{}; int denominator(原创 2015-11-03 15:07:20 · 565 阅读 · 0 评论 -
effective c++ 以对象管理资源
void f(){ Investment *p = creatInvestment(); //......... delete p;}1.如果在…中有一个return 2.在…中抛出了一个异常 3.或者delete动作位于某个循环中,被continue,或者goto 就会泄漏p指向的那块资源。 为确保creatInvestment()返回的资源总是被释放掉,我们需原创 2015-10-02 21:46:30 · 426 阅读 · 0 评论 -
effective c++为异常安全而努力是值得的(1)
如果我们假设函数带着“空白的异常明细”(empty exception specification)者必为nothrow函数int doSomething() throw();//空白的异常明细这并不是说doSomething绝对不会抛出异常,而是说如果doSomething抛出异常,将会是严重的错误,会有你意想不到的函数被调用 然后上cplusplus reference看看了关于set_une原创 2015-11-28 18:13:42 · 761 阅读 · 0 评论