随笔分类 - c++
摘要:模板是c++中灰常好用和强大的方法。传统的方法可以采用的方式来实现,现在尝试下采用模板方法来实现。//采用模板来写工厂方法#include class Product{public: Product() {std::cout class Factory{public: static Product * CreateProduct() { return new T(); }};class ProductA : public Product{public: ProductA() {std::cout ::CreateProduct(); dele...
阅读全文
摘要:lamda用法和函数对象,回调函数 部分用法对比#include #include #include #include #include #include #include #include #include using namespace std;//////////////////////////////////////////////////////////////////////////enum Conversion_Ch{ UPPER = 0, //大写 LOWER, //小写};/*** 仿函数*/class CFunctor{public: CF...
阅读全文
摘要:用法小demo,定义参见crt目录下的new头文件源码#include #include #include #include #include using namespace std;void __cdecl newhandler(){ cout << "The new_handler is called:" << endl; throw bad_alloc(); return;}int main(){ set_new_handler (newhandler); try { while (1) { new i...
阅读全文
摘要:std::bind用法实例#include #include using namespace std;typedef std::function fp;class A{public: virtual void f() { coutinit(); return 0;}std::function用法实例#include #include using namespace std;typedef std::function fp;void g_fun(){ cout fpi; //对于参数要使用占位符 std::placeholders::_1 ...
阅读全文
摘要:c c++宏定义中##使用方法#include typedef struct command{ char * name; void (*function) (void);}T_commands, *PT_commands;#define COMMAND(NAME) { #NAME, NAME##_command }void (f1_command) (void){ std::cout << "f1" << std::endl;}void (f2_command) (void){ std::cout << "f2" &l
阅读全文
摘要:先贴下,再回改#include "pub.h"template > class CONT = std::deque>class Stack{private: CONT elems;public: void push(T const &); void pop(); T top() const; bool empty() const { return elems.empty(); } template > class CONT2> Stack & operator = (Stack const &);};template c...
阅读全文
摘要:template inline const T& Max_(const T& v1, const T& v2){ return v1 > v2 ? v1:v2;}1.采用模板实现,泛型化2.inline 内敛提交运行速度3.const保证数据不改动4.引用节省空间,也提高运行效率
阅读全文
摘要:面向对象本质上仍然是面向过程,将变量和与变量属性相关的方法写在一起即成了封装,之所以这么设计,可以使业务逻辑更加合理,方法更具有专属性,管理起来也方便。必然的。在原有的基础上发展新的业务,可以通过继承一个类来实现,这样不用每个新类都要再写一遍了,共通的东西通过集成父类即可。集成时需要用到虚函数,体现了多态。模板可以编译成不同类型的代码,同样是一种多态形式。
阅读全文
摘要:dev c++编译器中需要在工程属性的参数中添加-std=c++11,可支持最新的c++标准。#include /* run this program using the console pauser or add your own getch, system("pause") or input loop */template T0 func(T1 v1, T3 v3, T4 v4){// std::cout << v1 <<"\n";// std::cout << v3 <<"\n";
阅读全文
摘要:思考:注册类的时候仍然是需要手动添加到初始化类中,有没有办法通过传递类名,或者以类字符串的形式注册新类?#include #include #include #include #include #include //#include "../purge.h"using namespace std;class Shape {public: virtual void draw() = 0; virtual void erase() = 0; virtual ~Shape() {}};//base classclass ShapeFactory{private: virtua..
阅读全文
摘要:1 方式一。改动基类的方式实现,这种方式再新类型加入时需要重新修改基类,不是最便捷的方式。//fatory#include #include #include #include #include using namespace std;class Shape{public: virtual void draw() = 0; virtual void erase() = 0; virtual ~Shape() {} class BadShapeCreation : public logic_error { public: BadShapeCrea...
阅读全文
摘要:/************************************************************************//* * @brief 将托管String转换为标准C++string *//************************************************************************/ref class GCStringToStdString{public: //************************************ // Method: operator() /...
阅读全文
摘要:class MemoryChunk{private: MemoryChunk* next;//指向下个内存块 void * mem;//指向可用的内存 size_t chunkSize;//该内存块的大小 size_t bytesAlreadyAllocated;//已经分配的字节数public: MemoryChunk(MemoryChunk *nextChunk ,size_t chunkSize); ~MemoryChunk(); inline void* alloc(size_t size); inline void free (void...
阅读全文
摘要:#include templateclass MemoryPool{public: MemoryPool(size_t size =EXPANSION_SIZE); ~MemoryPool(); //从空闲列表中分配T元素 inline void * alloc(size_t size); inline void free(void * someElement);private: MemoryPool* next; enum{EXPANSION_SIZE=2}; //将空闲元素添加至空闲列表 void expanTheFreeList(in...
阅读全文
摘要:#include class NextOnFreeList{public: NextOnFreeList *next;};class Rational {public: Rational(int a = 0, int b = 1) : n(a), d(b) {} inline void * operator new (size_t size); //over load new and delete operator inline void operator delete (void *doomed, size_t size); static void newMemPool() {ex...
阅读全文
摘要:1 单例模式下构建出来的对象本质上仍然是一个全局变量,因此全局变量可以完成同样的功能。2 需要考虑线程安全,避免new出2个对象。class SingleTon{private: int i_; SingleTon(int x) : i_(x) {} SingleTon &operator = (SingleTon &); SingleTon(const SingleTon &); //not allow copy and assign ~SingleTon(){}; static SingleTon *st_; static boost...
阅读全文
摘要:出错信息为:1> LINK : 已指定 /LTCG,但不需要生成代码;从链接命令行中移除 /LTCG 以提高链接器性能1>main.obj : error LNK2022: 元数据操作失败(8013119F): 现有的 TypeRef 应有对应的 TypeDef(dummy),但它没有: (0x01000020)。1>LINK : fatal error LNK1215: 元数据操作失败(8013119F):你只需要在一个cpp文件中添加namespace boost { struct boost::thread::dummy {};}即可通过编译。 可能是 bind展开后某
阅读全文
摘要:为防止迭代器失效,采用以下方法来避免。 for (pos = coll.begin(); pos != coll.end();) { if (pos->second == value) { coll.erase(pos++); } else { ++pos; } } map::iterator it = ShapeFactory::factories.begin(); while(it != ShapeFactory::factories.end()) delete it++->second;
阅读全文
摘要:using namespace std;class AddValue{private: int value_;public: AddValue(int v) : value_(v) { } void operator() (int &elem) const { elem += value_; }};int main(){ list coll; for (int i = 1; i <= 9; ++i) { coll.push_back(i); } for_each(coll.begin(), coll.en...
阅读全文
摘要:使用process explorer,注意,如果是使用release版本的话,需要将其对应版本的pdb文件导入进来,然后重启procexp,即可看到线程信息。
阅读全文
浙公网安备 33010602011771号