
c++笔记
sinshine
这个作者很懒,什么都没留下…
展开
-
const_cast
/*用法:const_cast (expression) 该运算符用来修改类型的const或volatile属性。除了const 或volatile修饰之外, type_id和expression的类型是一样的。 一、常量指针被转化成非常量指针,并且仍然指向原来的对象;原创 2011-09-02 16:15:12 · 7488 阅读 · 1 评论 -
内存分布
struct test{int a;int b;int c;};int main(int argc, char* argv[]){ test x; test y; printf("%x, %x, %x\n", &x.a, &x.b, &原创 2011-10-04 15:12:39 · 350 阅读 · 0 评论 -
static data members
在c++中,对于static data members,通过一个指针和通过一个对象来存取member的效果是一样的,而且这是唯一一样的situation。原创 2011-10-04 15:37:37 · 505 阅读 · 0 评论 -
已知一个序列seq=[a,b,....,z,aa,ab,...,zz,aaa,aab,....],求任意一个字符串s=[a-z]+在seq中出现的位置
// 26进制,不过有缺点,需要大数处理#include using namespace std;long pow(long x, long y){ long ret = 1; while(y>0) { ret = ret*x; --y; } re原创 2011-10-08 15:05:40 · 2312 阅读 · 0 评论 -
写宏定义:得到一个field在结构体(struct type)中的偏移量
#define OFFSETOF(type, field) ((size_t)&(((type *)0)->field))(type *)0:把0地址当成type类型的指针。((type *)0)->field:对应域的变量。&((type *)0)->field:转载 2011-10-10 13:42:28 · 1024 阅读 · 0 评论 -
some problems
const int x = 5;int main{ int x[x]; int y = sizeof(x)/sizeof(int);}display: 5int x = 5;int main(int argc, char* argv[]){ int x = x; cout<<x<<endl; return 0;}display:uninil原创 2011-10-25 14:38:38 · 795 阅读 · 2 评论 -
数组边界引起的死循环
int main(int argc, char* argv[]){ int i, a[10]; for (i = 1; i<=10; i++) a[i] = 0; return 0;}以上代码为死循环,如栈向低地址成长,那么a[10]实际为i,于是i赋值为0,进入死循环原创 2011-11-16 15:24:00 · 542 阅读 · 0 评论 -
0长度数组问题
标准c/c++不支持0长度数组,GNU支持struct node{ char a; short b; int c; char* d; char e[0]; }; int main(int argc, char* argv[]) { printf("%d\n", sizeof(struct node)); system("pause原创 2011-11-08 14:20:45 · 637 阅读 · 0 评论 -
词法分析中的贪心
C语言对多字符识别时,采用贪心,使用规则如下:每一个符号应该包含更多的字符。也就是说,编译器将程序分解成符号的方法是,从左到右一个字符一个字符地读入,如果该字符可能组成一个符号,那么再读入下一个字符,判断已经读入的两个字符组成的字符串是否有可能是一个符号的组成部分;如果可能,继续读入下一个字符,重复上述判断,直到读入的字符组成的字符串以不再可能组成一个有意义的符号。这个处理策略有事被称为“原创 2011-11-16 14:09:20 · 509 阅读 · 0 评论 -
c语言中的函数覆盖
在c程序设计中,不要让程序中的任何符号成为全局的,除非有意把他们作为程序的接口之一。如果不这样,就会因此产生重大bug,并且很难调试和发现。当编译器注意到库函数被另外一个定义覆盖时,它通常不会给出错误的信息。C语言的设计哲学——程序员所做的都是对的。原创 2011-12-05 10:13:11 · 3147 阅读 · 0 评论 -
反思程序中的代码和数据
在程序设计中,不要过分区分代码和数据,两者在一定情况下可以转换。很多著名的黑客软件,巧妙使用代码植入到数据区,然后运行自己的shell,操纵用户电脑。若以第三者来看,代码区和数据区说白了就是一系列的二进制,取决于你是怎么去看待这些个二进制数据。早期的internet蠕虫病毒,其繁殖的途径之一就是通过脆弱的finger防护进程。罪魁祸首是该进程中使用标准I/O库函数gets(), 它不检查缓冲区原创 2011-12-05 14:11:40 · 712 阅读 · 1 评论 -
相邻的字符串常量自动合并
在ANSI C中,相邻的字符串常量将被自动合并成一个字符串。除了最后一个字符串外,其余每个字符串末尾的'\0'字符会被自动删除(编译器的行为)#include #include int main(int argc, char *argv[]){ char* s1 = "hello""hello"; char* s2 = "hello" "hello"; char* s3原创 2011-12-04 12:51:00 · 1088 阅读 · 0 评论 -
c语言中的数组与指针
在c语言中,只有在函数定义的形式参数中,数组和指针是一致的。下面一段是c expert中的代码,我另外又加了一些。char ga[] = "abcdefghijklm";void my_array_func(char ca[10]){ printf(" value of ca = %#x \n", ca); printf(" addr of array param = %#x \n"原创 2011-12-06 14:22:16 · 669 阅读 · 1 评论 -
c语言 NULL
原创 2011-12-19 13:30:50 · 999 阅读 · 1 评论 -
接口类使用
假设有接口类Icommand,不同的模块有不同的接口:Acommand, Bcommand,Ccommand类均继承自Icommand而对于真正的使用场合,如下:某一场合需要Acommand:Icommand *Ic_pa = cmdFactory->Create("A");某一场合需要Bcommand:Icommand *Ic_pb = cmdFactory->Create("B")原创 2012-12-16 14:23:40 · 926 阅读 · 0 评论 -
继承的内存分布和代价
class B{private: int val; char c1; char c2; char c3;};class A1{private: int val; char c1;};class A2: public A1{private: c原创 2011-10-04 16:04:47 · 377 阅读 · 0 评论 -
inline关键字
inline int add(int a,int b)1.参数直接替换形参, int a = 1; int b = 2; int c = add(a, b);2.直接才编译过程中,能够判断出结果的,直接给出结果, int c = add(1,2);3.避免原创 2011-10-03 22:21:48 · 361 阅读 · 0 评论 -
C与C++的相互调用问题
#ifdef __cplusplusextern "C" {#endif int add(int a, int b); #ifdef __cplusplus}#endif int add(int a, int b){ return a+b;原创 2011-10-03 20:51:53 · 373 阅读 · 0 评论 -
class, struct, union
class my{public: struct { unsigned _a: 4; unsigned _b: 4; unsigned _c: 4; unsigned _d: 4; }; struct // 没有必要 { int _e; in原创 2011-09-02 17:30:59 · 547 阅读 · 0 评论 -
printf返回值
printf返回的值应该是打印的字符个数原创 2011-09-05 17:42:41 · 436 阅读 · 0 评论 -
define c/c++
#define hellosun "hello"#define B(x) #@x //char#define D(x) x##sun //union#define E(x) #x //string#define $ON 22 //$ can be usedint原创 2011-09-06 10:16:46 · 416 阅读 · 0 评论 -
operator++ 重载
class AA{private: int a;public: AA():a(0){}; AA& operator ++()// prefix { cout <<"++AA"<<endl; ++a; return *this; } AA opera原创 2011-09-06 17:12:40 · 1875 阅读 · 0 评论 -
define for flag
// the define below is just flags that indicate in or out of the argument// the define is empty#define _in#define _outvoid foo(_in int原创 2011-09-13 16:39:34 · 488 阅读 · 0 评论 -
enum
// enum is just like struct key wordenum week{sunday, monday, tuesday, wednesday, thirsday, friday, saturday, }; // extra comma in the end,原创 2011-09-15 13:19:16 · 397 阅读 · 0 评论 -
static_cast使用场合
用法:static_cast ( expression ) 该运算符把expression转换为type-id类型,但没有运行时类型检查来保证转换的安全性。它主要有如下几种用法: ①用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换。 进行上行转载 2011-08-17 21:49:42 · 542 阅读 · 0 评论 -
unsigned char, unsigned int
int main(int argc, char* argv[]){ unsigned int x = 10; int xx = -1; if (x > xx) cout<<"greater than"<<endl; else cout<<"less than"<<end原创 2011-09-15 17:31:27 · 576 阅读 · 0 评论 -
多重继承
#include using namespace std;class A{public: A(){}; void showA(){cout<<"hello A"<<endl;}};class B: public virtua原创 2011-09-29 15:26:34 · 329 阅读 · 0 评论 -
STL 组建
C++ STL六大组件:1、容器(Containers)2、算法(Algorithms)3、迭代器(Iterators)4、仿函数(Functors)5、配接器(Adapters)6、分配器(Allocators)原创 2011-09-29 16:55:44 · 488 阅读 · 0 评论 -
将有序数组转化为二叉树
using namespace std;struct node{ int val; struct node* left; struct node* right; node(int v=0) { val = v; left = 0; right = 0;原创 2011-10-11 21:45:13 · 1588 阅读 · 0 评论 -
observer模式
#include #include using namespace std;// observer pattern// 抽象类 纯虚函数class observer{public: virtual void update(int a, int b, int c原创 2011-09-29 16:18:36 · 541 阅读 · 0 评论 -
c++ const 重载 和 引用
class A{public: void foo(int &a) { cout<<"1"<<endl; } void foo(int a) { cout<<"2"<<endl; } const int fun() const// overload {原创 2011-10-13 21:57:53 · 712 阅读 · 0 评论 -
不相关成员函数的调用
#include using namespace std;class base{public: base(): a(0){} ~base(){}; void show() { cout << "th原创 2011-10-03 21:08:22 · 377 阅读 · 0 评论 -
c语言 全局(静态)和局部数组初始化
全局和静态数组根据规范要求初始化为0,但是局部数组如果不显示初始化,它的值不确定。code: result:原创 2013-11-14 17:44:09 · 6936 阅读 · 0 评论