C++11 auto_ptr 的问题
摘要:auto_ptr作为最早的智能指针,可以实现以RAII手法管理堆区对象,但它设计的本意只是简单的利用C++对于栈区对象的自动析构管理堆区对象, 并不像shared_ptr那样包含引用计数,可以在每次拷贝的时候多出一个“分身”。这时候,拷贝的语义就成了很大的问题(按理说直接禁掉可能好好些), 于是就出
阅读全文
std::vector<bool>中的坑
摘要:http://www.cplusplus.com/reference/vector/vector/?kw=vector C++中,vector<bool>为了达到节省内存的目的,专门做了特化,大概方式就是用bit位来存储数组中的元素。代价就是,这个容器里面的内置类型乱掉了: member type
阅读全文
sync_with_stdio
摘要:/* The synchronization referred to is @e only that between the standard * C facilities (e.g., stdout) and the standard C++ objects (e.g., * cout)....
阅读全文
What does it mean to “delegate to a sister class” via virtual inheritance?
摘要:Consider the following example:class Base {public: virtual void foo() = 0; virtual void bar() = 0;};class Der1 : public virtual Base {public: virtu...
阅读全文
pimpl idiom vs. bridge design pattern
摘要:http://stackoverflow.com/questions/2346163/pimpl-idiom-vs-bridge-design-pattern
阅读全文
为什么需要虚析构函数?
摘要:When should my destructor be virtual?When someone will delete a derived-class object via a base-class pointer.In particular, here’s when you need to m...
阅读全文
继承是为了被复用,而不是复用
摘要:一般在一个框架库里面,我们继承一个库里已有的类,但是又不想把它周边的一套完全重整,所以希望我们定义的这个类能像类库里那个类一样到处能用,这时候只要我们保证把类库里原有的public接口全部完整实现(或者直接用基类的),就可以做到这一点。在有友元存在的情况下,记住友元也是类的一部分,要连带一起定义新的...
阅读全文
How can I protect derived classes from breaking when I change the internal parts of the base class?
摘要:How can I protect derived classes from breaking when I change the internal parts of the base class?A class has two distinct interfaces for two distinc...
阅读全文
C++ friend keyword
摘要:You often need to split a class in half when the two halves will have different numbers of instances or different lifetimes. In these cases, the two h...
阅读全文
C++ 构造与析构函数
摘要:这两个概念并不对等,构造函数可以完全控制成员构造过程(通过初始化列表),析构函数准确说应该叫析构之前被调用的函数一般不应该手动调用析构函数:栈区对象会自动析构,堆区也是在delete的时候析构有一个特例,就是placement new的时候 void someCode() { ...
阅读全文
C++异常处理的问题
摘要:一般在C语言中,是通过返回值或者设置errno的方式来标识错误的但在C++里面,构造函数是没有返回值的,于是发明了异常的方式:为了正确的向使用者表明异常抛出的原因,你必须弄清楚异常抛出的原因(比如有的是内存分配的时候出错,有的是成员构造出错)还有一点需要注意,析构函数,从实现上不该抛异常的,因为析构...
阅读全文
C++ iostream的线程安全性问题
摘要:标准C里面的printf, fprintf之类的,会给FILE指针上锁(在unix平台上就是记录锁,按照msdn的说法windows上也有类似的锁),所以单次函数调用总是线程安全的:要注意,这里只对文件上锁,不是在操作过程中锁互斥量之类的,所以参数并不是线程安全的对于iostream,如果按照默认的...
阅读全文
C++指针比较的问题
摘要:在C++里面,指针的比较是要保障type-safe的,也就是说,这两个指针必须是convertible的:从一个指针能够直接转换到另一个指针(有中间路径不算,不然都往void*转没完没了),顺序不限比如任意类型的指针和void*比如指向基类的指针和指向派生类的指针又比如任意类型的指针和nullptr...
阅读全文
O_NONBLOCK on regular file
摘要:It seems that writes/reads to regular files can't not be made non-blocking. I found the following references for support:from The Linux Programming In...
阅读全文
C++11 不抛异常的new operator
摘要:在google cpp style guide里面明确指出:we don't use exceptionsC++11的noexcept关键字为这种选择提供了便利。C++11以前,提及malloc和new的区别,总是会强调由malloc返回的指针需要检查是不是null,因为空间分配可能失败,而由new...
阅读全文
In p = new Fred(), does the Fred memory “leak” if the Fred constructor throws an exception?
摘要:No.If an exception occurs during the Fred constructor of p = new Fred(), the C++ language guarantees that the memory sizeof(Fred) bytes that were allo...
阅读全文
method chaining
摘要:经常写Java的话,可能比较熟悉下面这种函数调用方式 object.method1().method2()术语叫所谓的method chaining,在c++里面,为了支持这种调用格式,你必须保障函数以引用类型返回*this
阅读全文
C++中的运算符重载
摘要:运算符重载给c++提供了很强大的功能,使得我们可以将一个类设计得跟内置类型差不多了。但是运算符这种东西最大的问题就是,看起来直观,却常常隐匿了实际的函数调用过程,时常容易让人困惑。所以在google c++编码规范里面,建议直接用普通的成员函数而不是重载运算符。可以看下面的代码段: class...
阅读全文