摘要: 关键字:Const,Const函数,Const变量,函数后面的Const看到const 关键字,C++程序员首先想到的可能是const 常量。这可不是良好的条件反射。如果只知道用const 定义常量,那么相当于把火药仅用于制作鞭炮。const 更大的魅力是它可以修饰函数的参数、返回值,甚至函数的定义体。const 是constant 的缩写,“恒定不变”的意思。被const 修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性。所以很多C++程序设计书籍建议:“Use const whenever you need”。1. 用const 修饰函数的参数如果参数作输出用,不论它是什. 阅读全文
posted @ 2014-03-29 16:45 DaiHong 阅读(568) 评论(0) 推荐(0) 编辑
摘要: 1. 先看代码例子(1)void GetMemory(char* p){ p = (char*)malloc(100);}void Test(void){ char* str = NULL; GetMemory(str); strcpy(str, "hello world"); printf(str);}(2)void GetMemory2(char** p, int num){ *p = (char*)malloc(num);}void Test(void){ char* str = NULL; GetMemory(&str, 100); strcpy(str, 阅读全文
posted @ 2014-03-29 14:54 DaiHong 阅读(627) 评论(1) 推荐(0) 编辑