随笔分类 -  C/C++

摘要:template char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];#define _countof(_Array) (sizeof(*__countof_helper(_Array)) + 0) 阅读全文
posted @ 2013-11-19 19:57 avexer 阅读(168) 评论(0) 推荐(0)
摘要:This topic describes how to write a move constructor and a move assignment operator for a C++ class. A move constructor enables you to implement move semantics, which can significantly improve the performance of your applications. For more information about move semantics, see Rvalue Reference Decla 阅读全文
posted @ 2013-10-21 22:46 avexer 阅读(416) 评论(0) 推荐(0)
摘要:Generated by command:clang -dM -E -x c /dev/null#define _X86_ 1#define __ATOMIC_ACQUIRE 2#define __ATOMIC_ACQ_REL 4#define __ATOMIC_CONSUME 1#define __ATOMIC_RELAXED 0#define __ATOMIC_RELEASE 3#define __ATOMIC_SEQ_CST 5#define __CHAR16_TYPE__ unsigned short#define __CHAR32_TYPE__ unsigned int#define 阅读全文
posted @ 2013-10-17 17:28 avexer 阅读(841) 评论(0) 推荐(0)
摘要:Generated by command: gcc -dM -E -x c /dev/null#define __DBL_MIN_EXP__ (-1021)#define __pentiumpro__ 1#define __UINT_LEAST16_MAX__ 65535#define __ATOMIC_ACQUIRE 2#define __FLT_MIN__ 1.17549435082228750797e-38F#define __UINT_LEAST8_TYPE__ unsigned char#define __INTMAX_C(c) c ## LL#define __CHAR_BIT__ 阅读全文
posted @ 2013-10-17 17:24 avexer 阅读(794) 评论(0) 推荐(0)
摘要:多个线程读写同一个shared_ptr对象需要加锁(注意分清shared_ptr对象与shared_ptr管理的对象)。MutexLock mutex;shared_ptr globalPtr;void read(){ shared_ptr localPtr; { MutexLockGuard lock(mutex); localPtr = globalPtr; } // ...}void write(){ shared_ptr newPtr(new Foo()); { MutexLockGuard lock(mut... 阅读全文
posted @ 2013-10-08 07:26 avexer 阅读(287) 评论(0) 推荐(0)
摘要:This keyword can only be applied to non-static and non-const data members of a class. If a data member is declared mutable, then it is legal to assign a value to this data member from a const member function.For example, the following code will compile without error because m_accessCount has been de 阅读全文
posted @ 2013-09-25 22:36 avexer 阅读(226) 评论(0) 推荐(0)
摘要:tolua++-1.0.93目前还尚不支持lua5.2,只能与lua5.1配合了(google code上有lua for windows,安装后再设置一下vc的include和lib目录即可)。然后再开始编译tolua,新建一个静态库的工程,src/lib目录下的文件拖进去,OK。tolua++.exe可以直接在网上下载,反正我自己懒得编译了(src/bin)。它的作用就是根据我们编写的pkg文件,生成一个cpp文件,我们再将这个生成的cpp文件拉进我们的工程一起编译。来个Demo:1. 先写个c++类:// hello.h#include class hello{public: vo... 阅读全文
posted @ 2013-09-22 20:24 avexer 阅读(407) 评论(0) 推荐(1)
摘要:平时在读写文件时,我习惯了WIN32和C函数,今天试着用C++的风格来处理了一下。注意operator>中被我注释掉的代码,因为os#include struct user{ char name[32]; int age;};std::ostream& operator>(std::istream & is, user& user){ //is>>user.name>>user.age; is.read(user.name, sizeof(user.name)); is.read((char*)&user.age, sizeo 阅读全文
posted @ 2013-09-17 19:52 avexer 阅读(494) 评论(0) 推荐(0)
摘要:1.auto关键字试想,曾经每次迭代vector之类的容器时,是否写了很长的迭代器类型声明:std::vector::iterator iter = vec.begin();好了,有了auto后可以直接这么写:auto iter = vec.begin();另外,auto还可以作为函数的返回类型,例如:templateauto add(const T1 v1, const T2 v2) -> decltype(v1 + v2){ return v1 + v2;}2.nullptr曾经,我们一般在初始化指针的时候都是直接初始化为0,现在可以用nullptr才初始化,就这方面来说它们是没有区 阅读全文
posted @ 2013-08-15 20:07 avexer 阅读(401) 评论(0) 推荐(0)
摘要:#define xx(a) const char * _cstr##a = "xxxx";#define yy(a) #aint _tmain(int argc, _TCHAR* argv[]){ xx(bbb); std::cout xxxx std::cout fuck std::coutggggggg std::cout"hhhhhhhhhh\n\r\t"std::cout hhhhhhhhhh然后三个换行 return 0;}##一般在宏中构造一个变量名#用于产生一个字符串,跟在#后面的字符会作为字符串对待 阅读全文
posted @ 2013-08-06 11:56 avexer 阅读(214) 评论(0) 推荐(0)
摘要:cast between different pointer to member representations, compiler may generate incorrect codeAn incorrect cast was detected.C4407 can be generated because of compiler conformance work that was done in Visual C++ 2005. Pointer-to-member now requires a qualified name and the address-of operator (& 阅读全文
posted @ 2013-07-03 20:41 avexer 阅读(394) 评论(0) 推荐(0)
摘要:有时候类里面定义了很多int,char,struct等C语言里的那些类型的变量,我习惯在构造函数中将它们初始化为0,但是一句句的写太麻烦,所以直接就memset(this, 0, sizeof *this);将整个对象的内存全部置为0。对于这种情形可以很好的工作,但是下面几种情形是不可以这么使用的:1.类含有虚函数表:这么做会破坏虚函数表,后续对虚函数的调用都将出现异常2.类中含有C++类型的对象:例如,类中定义了一个list的对象,由于在构造函数体的代码执行之前就对list对象完成了初始化,假设list在它的构造函数里分配了内存,那么我们这么一做就破坏了list对象的内存。 阅读全文
posted @ 2013-02-07 09:24 avexer 阅读(241) 评论(0) 推荐(0)
摘要:如果大家用的是VC2010及以上版本的编译器的话,可以直接使用C++ 0x标准中的static_assert,这些编译器都是支持这个新标准的。而我好奇的是boost是怎么实现这个的,非常简单,就几行代码。#define BOOST_STATIC_ASSERT( B ) \ typedef ::boost::static_assert_test)>\ BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__)// HP aCC cannot deal with missing names for template value para... 阅读全文
posted @ 2013-01-27 16:32 avexer 阅读(1069) 评论(0) 推荐(0)
摘要:一、构造函数初始化列表的异常机制class demo{public: demo(int size) try : _array(new int[size]) { } catch (bad_alloc&) { }private: int *_array;};假设在分配内存时发生了异常,并且被我们catch到,但是,这个异常仍然会继续向外抛,以让调用者知道。可以这么说,编译器在每个catch块的最后又为我们加了一行throw;因此,安全起见,在构造这个对象的代码处,还需再写一次try,catch。try{ demo obj;}catch (bad_alloc&){}二、构造函数体中的 阅读全文
posted @ 2013-01-12 14:58 avexer 阅读(126) 评论(0) 推荐(0)
摘要:有下面两个类:class base{};class sub : public base{};下面的异常代码:try{ base *bb = new sub(); throw(bb);}catch (sub*){ std::cout<<”this handler won’t execute”;}catch (base*){ std::cout<<”exxcute this handler”<<std::endl;}因此,最好不要throw指针类型的异常。 阅读全文
posted @ 2013-01-12 13:04 avexer 阅读(113) 评论(0) 推荐(0)
摘要:默认析构函数与空析构函数并不完全等价 !完整类型 ! 阅读全文
posted @ 2013-01-11 22:48 avexer 阅读(317) 评论(0) 推荐(0)
摘要:在看boost相关的资料时,看到一个术语“完整类型”,以前一直没注意过这个。所谓的C++完整类型,就是说,仅仅有类型的前向声明时不够的,必须要有类的完整定义。// fuck.hclass shit;class fuck{public:fuck(); //~fuck();private: std::auto_ptr _pshit;};在fuck这个类中中,没有定义析构函数,编译器会提供一个默认的析构函数,析构auto_ptr对象,进而delete shit。但是,由于在此,只有shit的声明,却没有它的完整定义,因此shit的析构函数将不会被调用。编译器也会提示C4105这个警告。为了避免这样的 阅读全文
posted @ 2013-01-11 21:45 avexer 阅读(247) 评论(0) 推荐(0)
摘要:1.下载 & 解压2.cdSTLport-5.2.13.configure msvc9 --with-dynamic-rtl4.cd build/lib5.nmake clean install6.将STLport-5.2.1\stlport和STLport-5.2.1\lib分别添加到VS2008的include和lib目录中去可以新建个项目测试一下,默认情况下会动态链接到stlport,要想静态链接需要#define _STLP_USE_STATIC_LIB。至于上面步骤3中的with-dynamic-rtl参数,因为默认情况下stlport编译产生的lib,只能与静态的CRT进行 阅读全文
posted @ 2012-12-24 20:29 avexer 阅读(156) 评论(0) 推荐(0)
摘要:为了便于输出_x和_y的值,直接将它们声明为public。如果在子类中主动调用父类的构造函数将会发现输出的_x的值为0。class base_class{public: base_class(int val = 0) : _x(val) { std::cout<<"base_class() con"<<std::endl; } base_class(const base_class &rhs) : _x(rhs._x) { std::cout<<"base_class() copy"<<std::e 阅读全文
posted @ 2012-11-27 20:23 avexer 阅读(126) 评论(0) 推荐(0)
摘要:Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called.To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keywo 阅读全文
posted @ 2012-11-13 21:16 avexer 阅读(126) 评论(0) 推荐(0)