C/C++
文章平均质量分 59
hi5000
这个作者很懒,什么都没留下…
展开
-
VC结构体边界对齐
在VC中,结构中各种数据类型有不同的对齐方式: char 对齐偏移量必须是1的倍数 short 对齐偏移量必须是2的倍数 int 对齐偏移量必须是4的倍数 float 对齐偏移量必须是4的倍数 double 对齐偏移量必须是8的倍数 为了确保结构的大小为结构字节的边界数,(即该结构中占用最大空间的类型所占用的字节数)的倍数 所以在原创 2012-06-02 13:46:30 · 1033 阅读 · 0 评论 -
new和delete数组类型
当new自定义类型数组对象的时候,会在返回地址的前面多申请4个字节,存放对象的个数,delete的时候根据对象个数循环调用析构函数,如果对象delete的时候没有析构函数可以使用,那么这个长度信息是不会存在的。 #include class test { public: test(){ std::cout << "constructor" << std::endl; } ~t原创 2012-06-21 20:53:01 · 799 阅读 · 0 评论 -
set_unexpected使用
VS2005中的set_unexpected与标准C++不同,在exception头文件中set_unexpected函数定义为空,MSDN对set_unexpected的解释如下 The C++ Standard requires that unexpected is called when a function throws an exception that is not on its原创 2012-06-20 20:12:18 · 1577 阅读 · 0 评论 -
适配器初识
#include #include #include #include using namespace std; //缺省版本 bool count_f(const int a) { return a > 8; } //ptr_fun版本 bool greater_f(const int a, const int b) { return a > b; }原创 2012-06-20 18:05:54 · 333 阅读 · 0 评论 -
new和delete的使用
1、操作符与表达式 #include using namespace std; class A { public: A():a(100),b(200) { std::cout << "constructor " << std::endl; } ~A() { std::cout << "destructor " <<原创 2012-06-15 19:59:02 · 379 阅读 · 0 评论 -
虚继承下的多重继承内存布局
#include class A { public: int a; A():a(10){ std::cout << "A constructor" << std::endl; } void f() { std::cout << "A::f" << std::endl; } }; class B: virtual public A { p原创 2012-06-24 12:16:34 · 390 阅读 · 1 评论 -
多态虚函数表
#include class A { public: virtual void f() { std::cout << "A::f()" << std::endl; } virtual void g() { std::cout << "A::g()" << std::endl; } virtual void原创 2012-06-23 17:36:15 · 281 阅读 · 0 评论 -
多继承下的内存布局
#include class A { public: int a; virtual void f() { std::cout << "A::f()" << std::endl; } virtual void g() { std::cout << "A::g()" << std::endl; } vi原创 2012-06-23 19:25:19 · 370 阅读 · 0 评论 -
模板函数与类模板
#include #include using namespace std; int max_f(int a, int b) { std::cout << "int max_f(int a, int b)" << std::endl; return a > b ? a : b; } template T max_f(T a, T b) { std::cout原创 2012-06-22 19:02:06 · 409 阅读 · 1 评论 -
C隐式类型转换与运算符优先级
1、隐式类型转换 右边的类型要作运算之前都先要转换为左边的类型,然后再进行算术运算,不同类型之间运算按照从下到上进行类型转换 double ^ | long ^ | unsigned ^ | int 2、运算符优先级(转) 共44个运算符:醋坛酸味灌,味落跳福豆 醋-初等,4个: ( ) [ ] -> 指向结构体成员 .原创 2012-06-06 20:25:53 · 831 阅读 · 0 评论 -
指针使用汇总
#include int main() { char* a[10]; //a是一个数组,数组里面每个元素是一个char*类型的指针 char* (*b)[10];//b是一个指针,该指针指向一个数组,数组里面每个元素是一个char*类型的指针 char** c[10]; //c是一个数组,数组里面每个元素是一个char**类型的指针(也就是指向指针的指针)原创 2012-05-26 15:06:48 · 304 阅读 · 0 评论 -
new的一个可能实现
#include using namespace std; new_handler g_new_handler_f = NULL; class A { public: A(){ std::cout << "constructor" << std::endl; /* throw int();*/} ~A(){ std::cout << "destructor" << std::原创 2012-06-21 14:49:10 · 312 阅读 · 0 评论 -
static与const小结
1、static变量 函数体内:静态变量,在函数第一次被调用的时候定义该变量,以后调用该函数的时候直接使用上次定义的变量,变量会一直存在,直到程序结束 函数体外:一个声明为静态的变量可以被模块内所有函数所访问,但不能被模块外的函数所访问。 类静态成员变量:属于类所有,可以直接使用类名调用,所以属于该类的对象共同拥有。 2、static函数 静态函数:声明为静态的函数可以被模块内所有其原创 2012-06-06 19:16:17 · 741 阅读 · 0 评论 -
c++小知识
1、a++不能作为左值 int a = 10; ++a = 1;//ok a++ = 1;//errora++是先使用a的值作运算,然后再使a的值增加1,如果允许执行a++左值的赋值运算,那么就要在赋值运算之后使a值再增加1,这样赋值运算就没意义了,a最终的结果不是赋值后的结果。 2、#error指示符 指令将使编译器显示一条错误信息,然后停止编译 3、AS原创 2012-06-08 17:04:52 · 344 阅读 · 1 评论 -
c++冷门关键字
1、volatile 定义为volatile的变量可能会被意想不到的被改变,所以每次使用的时候优化器都会重新的去读取该变量的值,而不是使用保存在寄存器中的值 volatile 修饰函数的时候和const类似,如volatile定义的类对象只能调用volatile修饰的成员函数,volatile的成员函数和非volatile的同名同参成员函数会构成重载 2、mutable 可以在类的const成原创 2012-06-07 16:36:37 · 482 阅读 · 1 评论 -
不同进制输出
#include #include #include #include #include using namespace std; int main() { bool b = true; std::cout << b << std::endl; //1 std::cout << boolalpha << b << std::endl;原创 2012-06-15 15:41:17 · 377 阅读 · 0 评论 -
默认数据类型的最大最小值
#include #include using namespace std; int main() { std::cout << "int max: \t" << INT_MAX << std::endl; std::cout << "int min: \t" << INT_MIN << std::endl; std::cout << "long原创 2012-06-15 15:53:06 · 295 阅读 · 0 评论 -
流迭代器初识
#include #include #include #include #include using namespace std; int main() { vector a; vector::iterator iter; for (int i=0; i<10; i++) a.push_back(i); ostream_iterat原创 2012-06-15 12:47:28 · 233 阅读 · 0 评论 -
指定小数位数输出
按有效位输出是 setprecision,按小数位数输出也是setprecision,但到底是谁取决于fixed。 cout cout 测试代码: #include #include using namespace std; int main( void ) { const double value = 12.3456789; cout << value <转载 2012-06-17 09:49:50 · 705 阅读 · 0 评论 -
虚基类构造函数调用
#include using namespace std; class A { public: A(int a):a_(a){ std::cout << "A constructor" << std::endl; } private: int a_; }; class B: virtual public A { public: B(int a, int b):原创 2012-06-16 12:43:49 · 917 阅读 · 0 评论 -
使用dyncall动态调用函数
#include #include "include/dyncall.h" class test { public: test(int x):value(x){} void print(){ std::cout << value << std::endl; } private: int value; }; char* show(int x, double y, bo原创 2012-10-20 10:29:21 · 1614 阅读 · 4 评论