class. • Two Kinds of Smart Pointer: shared and unique More about Smart Pointer • A decorator (wrapper) of raw pointer. • A template class. • Two Kinds of Smart Pointer: shared and unique
class. • Two Kinds of Smart Pointer: shared and unique More about Smart Pointer • A decorator (wrapper) of raw pointer. • A template class. • Two Kinds of Smart Pointer: shared and unique
boost::shared_ptr should behave the same. • According to the Wikipedia C++0x page: The TR1 implementation lacked certain pointer features such as aliasing and pointer arithmetic, but the C++0x version will add these. • Now, C++11 provides std::unique_ptr, as well as improvements to std::shared_ptr and std::weak_ptr from TR1. std::auto_ptr is deprecated. • In generally, there is little different between std and boost on smart pointer operations. C++0x VS Boost • std::shared_ptr is the C++0x form of tr1::shared_ptr, and boost's boost::shared_ptr should behave the same. • According to the Wikipedia C++0x page: The TR1 implementation lacked certain pointer features such as aliasing and pointer arithmetic, but the C++0x version will add these. • Now, C++11 provides std::unique_ptr, as well as improvements to std::shared_ptr and std::weak_ptr from TR1. std::auto_ptr is deprecated. • In generally, there is little different between std and boost on smart pointer operations.
do one thing: variable num multiply itself 10,000 times. Performance • Define a class with a double variable. • Just do one thing: variable num multiply itself 10,000 times.
• 2. Multiplying. • 3.Freed. • !Important!: Each multiplying will multipy a double variable for 10,000 times. • What does the program do? • 1. Allocation. • 2. Multiplying. • 3.Freed. • !Important!: Each multiplying will multipy a double variable for 10,000 times.
• Delete • Invoke methods of class • Get the raw pointer • swap • Create a shared pointer object • weak pointer • Delete • Invoke methods of class • Get the raw pointer • swap
avoids compiler to construct object using implicit conversion. For example: shared_ptr<MyClass> obj = new MyClass(); In the case without using explicit key word: obj will convert to shared_ptr type. More details in 《More Effective C++》
take cGeDBIT as an example: • Vector: Usage (Create) • To create a smart pointer object, just: • Or take cGeDBIT as an example: • Vector: Be carefull!!
freed “Child1” and “Leaf” • So, weak_ptr appears Usage (weak pointer) Root • In the case like: • It is impossible to freed “Child1” and “Leaf” • So, weak_ptr appears Child1 Child2 Child3 Leaf
which points to the object owned by shared_ptr without having an increment in the reference counter. Usage (weak pointer) • What is weak_ptr: • weak_ptr is a companion pointer which points to the object owned by shared_ptr without having an increment in the reference counter.
static_cast<newType*>(expression) instead of using (type*) (expression) • Safe & Readable • Use C++-style type conversion • Use static_cast<newType*>(expression) instead of using (type*) (expression) • Safe & Readable
Codes are read by human!!! • Tidy and readable is so important! • Codes are written by human. • Codes are read by human!!! • Tidy and readable is so important!
My hands are on the keyboard, I don’t want to drag the scroll bar..... • https://github.com/facebook/folly/ • https://github.com/chenshuo/muduo-protorpc Some advices(Code Writing) • Max length of a line 80 or 120 • My hands are on the keyboard, I don’t want to drag the scroll bar..... • https://github.com/facebook/folly/ • https://github.com/chenshuo/muduo-protorpc
system files. • 3、C++ system files. • 4、Other libraries’s .h files • 5、Your project’s .h files More advices(Code Style) • The order of #include s. • 1、class.h • 2、C system files. • 3、C++ system files. • 4、Other libraries’s .h files • 5、Your project’s .h files