const 在C++里与C语言的一些不同
---- 代码说明详尽
#include <iostream> #include <ctime> #include <cstdlib> #include <string> using namespace std ; // C++增强的const namespace LOL { namespace DNF { int a = 1 ; void func() { cout<<"hello world!"<<endl; } } } void Review() // 复习下命名空间 { using namespace LOL::DNF ; // 声明这个命名空间 否则就要在变量里面添加:: cout<<a<<endl; func(); // malloc 类型强制转换 int qqq = 5201314 ; int * ppp = (int* )malloc(sizeof(int *)) ; ppp = &qqq ; cout<<(*ppp)<<endl; } // const 增强的地方 void textConst() { /* const int a = 10 ; // 全局区 void text01(){ int * p = &a ; *p = 1000 ; } // C语言中这样子这个值就被修改了 语法正确 运行报错 , 但是 如果是在C++中,必报错 */ // 出现以上问题的原因大概就是,const 被存放在栈区,是一个伪常量 const int a = 10 ; int *b = (int*)&a ; *b = 100 ; cout<<*b<<endl; //在如上的a中 , a的数据类型是 const int,想修改就必须改成 int * cout<<a<<endl; /*原理就是const做了多一点手脚 cosnt 是利用键值对 即字典 的形式存储到常量区的是不能够被修改的 , 我只是创建了一个临时的 temp值 让一个指针p指向value*/ } void textConst02() { // const 在C语言中默认是外部链接属性 在C++中默认是内部链接属性 /* //在C语言中份文件编写,用extern可以直接引用这个文件 如在 game.c文件中 const int game = 100 ; 那么在main.c里面就可以直接 int main(void) { extern const int game ; printf("%d\n",game) ; } //而且输出的是正确的数值 */ // 但是在C++中不能够这样子使用 原因就是 在C++中const默认是内部链接属性 // 如果要像C语言中,可以用 extern const int game = 10 ; } void textConst03() { //const 变量内存分配的情况 //如果对const取地址 则分配内存 const int mm_aa = 100 ; int (*p) = (int*)&mm_aa ; (*p) = 1000 ; cout<<"mm_aa = "<<mm_aa<<endl; //如果使用变量来初始化 int nn_qq = 100 ; const int nn_aa = nn_qq ; cout<<"const nn_qq = "<<nn_aa<<endl; p = (int*)&nn_aa ; *p = 999 ; cout<<"修改之后的值cout nn_aa = "<<nn_aa<<endl; // 简而言之 用变量来初始化,会给const分配内存,那么就可以修改 } struct Person1 { string Name ; int age ; }; void textConst04() { struct Person { string Name ; int age ; }; const struct Person *p1 = (Person*)malloc(sizeof(64)); //const struct Person1 *p2 = (Person*)malloc(sizeof(1000)) ; Person * p3 = (Person*)&p1 ; (*p3).Name = "叔叔呢" ; (*p3).age = 18 ; cout<<"名字是:"<<(*p3).Name<<"年龄是:"<<(*p3).age<<endl; cout<<"名字是:"<<p3->Name<<"年龄是:"<<p3->age<<endl; } void text01() { int b = 3 ; const int a = b ; int * p = const_cast<int*>(&a) ; *p = 999 ; cout<<a<<endl; } int main(void) { //Review(); //textConst() ; //textConst02(); //textConst03(); //textConst04() ; }
第二天
#include <iostream> #include <string> #define MIN_A 988 using namespace std ; namespace A { #define MAX_A 1000 } void textConst() { // #define没有数据类型 ,不管是什么作用域,都可以使用 cout<<MAX_A<<endl; #undef MAX_A // 一般使用undef来关掉这个define } // 两个同名的函数但是不数据类型的函数可以同时存在 void fun(int num) { cout<<"调用的是int 树蕨类型"<<num<<endl; } void fun(short num) { cout<<"调用的是short 树蕨类型"<<num<<endl; } void textConst02() { fun(MIN_A); // define 默认调用的是int数据类型 cout<<"加了const short 的数据类型之后"<<endl; const short min_a = 111 ; fun(min_a) ; } namespace textConstNamespace { void fun01(int num) { cout<<"调用的的是int类型"<<endl; } void fun01(short num) { cout<<"调用的的是short 类型"<<endl; } const int num1 = 1 ; const short num2 = 2 ; } void textConst03() { using namespace textConstNamespace ; fun01(num1) ; fun01(num2) ; } namespace textConstNamespace02 { void textConst() { int age = 100 ; const int age2 = age ; int * p = (int*)&age2 ; *p = 998 ; cout<<"const的死亡测试,修改后 "<<age2<<" 修改前 "<<age<<endl; } } int main(void) { //textConst() ; //textConst02() ; //textConst03() ; //textConstNamespace02::textConst() ; }