您的位置:首页 > 编程语言 > C语言/C++

Effective C++ 3nd 读书摘要(九、杂项讨论)Item53 - 55

2009-06-01 17:42 561 查看
九、杂项讨论
Item53. 不要轻忽编译器的警告
比如:

class B {
public:
 virtual void f() const;
};

class D: public B {
public:
 virtual void f();
};


编译器会警告:
warning: D::f() hides virtual B::f()
因为D中的f()没有const。



Item54. 让自己熟悉包括TR1在内的标准程序库
TR1组件有:
1. The smart pointers TR1::shared_ptr 和 tr1::weak_ptr. TR1::shared_ptrs
2. tr1::function(Item35)
3. tr1::bind(Item35)

其他TR1组件划分为两组:
①Hash tables、Regular expressions、Tuples、tr1::array、tr1::mem_fn、tr1::reference_wrapper、Random number generation、Mathematical special functions、C99 compatibility extension
②Type traits、tr1::result_of

简单示范:
①tr1::function

void registerCallback(std::tr1::function<std::string (int)> func);
            // the param "func" will take any callable entity
            // with a sig consistent with "std::string (int)"




ITem55. 让自己熟悉Boosthttp://boost.org
Boost程序库的主题主要有:
String and text processing、Containers、Function objects and higher-order programming、Generic programming、Template metaprogramming(TMP)、Math and numerics、Correctness and testing、Data structures、Inter-language support、Memory、Miscellaneous

简单示范:
①Function objects and higher-order programming

using namespace boost::lambda;                    // make boost::lambda functionality visible
std::vector<int> v;
...
std::for_each(v.begin(), v.end(),                 // for each element x in
              std::cout << _1 * 2 + 10 << "/n");  // v, print x*2+10;
                      // "_1" is the Lambda library's placeholder for the current element




②Template metaprogramming(TMP)

// create a list-like compile-time container of three types (float,
// double, and long double) and call the container "floats"
typedef boost::mpl::list<float, double, long double> floats;

// create a new compile-time list of types consisting of the types in
// "floats" plus "int" inserted at the front; call the new container "types"
typedef boost::mpl::push_front<floats, int>::type types;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: