09 2012 档案

摘要:shared_ptr很好地消除了显式的delete调用,如果读者掌握了它的用法,可以肯定delete将会在你的编程字典中彻底消失 。但这还不够,因为shared_ptr的构造还需要new调用,这导致了代码中的某种不对称性。虽然shared_ptr很好地包装了new表达式,但过多的显式new操作符也是个问题,它应该使用工厂模式来解决。因此,shared_ptr在头文件<boost/make_shared.hpp> 中提供了一个自由工厂函数(位于boost名字空间)make_shared<T>(),来消除显式的new调用,它的名字模仿了标准库的 make_pair(),声明 阅读全文
posted @ 2012-09-29 14:21 瑞尼书苑 阅读(5190) 评论(0) 推荐(0) 编辑
摘要:按照默认规定,只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应数据类型的数据转换为该类对象,如下面所示:class String {String ( const char* p );//用C风格的字符串p作为初始化值//…}String s1 = “hello”;//OK 隐式转换,等价于String s1 = String(“hello”);但是有的时候可能会不需要这种隐式转换,如下:class String { String ( int n );//本意是预先分配n个字节给字符串String ( const char* p );//用C风格的字符串p作为初始化值//…}下面两种 阅读全文
posted @ 2012-09-29 14:11 瑞尼书苑 阅读(199) 评论(0) 推荐(0) 编辑
摘要:1. 获取时间戳:time_t rawtime;time(&rawtime); //结果是时间戳2. 将时间戳转为 char* 类型的日期Www Mmm dd hh:mm:ss yyyy 1 /* ctime example */ 2 #include <stdio.h> 3 #include <time.h> 4 #include <iostream> 5 6 using namespace std; 7 8 int main () 9 {10 time_t rawtime;11 12 time ( &rawtime );13 printf 阅读全文
posted @ 2012-09-29 11:54 瑞尼书苑 阅读(7962) 评论(0) 推荐(0) 编辑
摘要:有时候经常要实现程序在某个时间段进行某些操作的功能,如果利用boost库的话可以很方便就能实现。[cpp] #include<boost/date_time/posix_time/posix_time.hpp> #include<boost/date_time/gregorian/gregorian.hpp> //首先要定义一个时间区间段,设程序需在每日的8:50~9:15之间实现某些操作; boost::posix_time::time_period*pTimePeriod=NULL; boost::posix_time::ptimept(b... 阅读全文
posted @ 2012-09-28 17:38 瑞尼书苑 阅读(684) 评论(0) 推荐(0) 编辑
摘要:可以设置函数属性(Function Attribute), 变量属性(Variable Attribute), 类型屬性(Type Attribute)语法: __attribute__ (parameter) 注: attribute 前后各有两个下划线(underscore) 后面紧跟小括号((bracket)), 括号中间为__attribute__参数.其位置一般于声明的尾部; 以前函数属性(Function Attribute) 函数属性可以帮助开发者把一些特性添加到函数声明中,从而可以使编译器在错误检查方面的功能更强 大。__attribute__机制也很容易同非GNU应用程序做到 阅读全文
posted @ 2012-09-27 16:59 瑞尼书苑 阅读(152) 评论(0) 推荐(0) 编辑
摘要:#include <string>//使用C++标准库的string类时using namespace std;//同上#include <sstream>#include <iostream>#include <stdlib.h>//要将string类和int类型直接转换最好有这些包含,//因为自己写一个转换函数比较方便,函数定义参考如下string getstring ( const int n ){std::stringstream newstr;newstr<<n;return newstr.str();}////////// 阅读全文
posted @ 2012-09-27 14:19 瑞尼书苑 阅读(213) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示