`
jgsj
  • 浏览: 964682 次
文章分类
社区版块
存档分类
最新评论

关于const关键字作用范围和使用效果

 
阅读更多
关于const关键字作用范围和使用效果的讨论:
const是使一个变量变成一个常数。使所定义的变量的值不能被改变。但当const作用一个指针变量或一个函数时就不清楚其真正的作用效果了。测试以下程序初步判断其作用。
程序代码:
(1)
#include<iostream>
using namespace std;
void main()
{ char cc[]="asdf";
char dd[]="jkl";
const char *p;
char const *a;
char * const b;
cout<<p<<endl<<a<<endl<<b<<endl;
}
调试结果:
C:/Documents and Settings/is54/wjh.cpp(9) : error C2734: 'b' : const object must be initialized if not extern
(2)
#include<iostream>
using namespace std;
void main()
{ char cc[]="asdf";
char dd[]="jkl";
const char *p=cc;
char const *a=cc;
char * const b=cc;
cout<<p<<endl<<a<<endl<<b<<endl;
}
调试结果:
通过。
运行结果:
asdf
asdf
asdf
Press any key to continue
(3)
#include<iostream>
using namespace std;
void main()
{ char cc[]="asdf";
char dd[]="jkl";
const char *p=cc;
char const *a=cc;
char * const b=cc;
cout<<p<<endl<<a<<endl<<b<<endl;
p=dd;
a=dd;
b=dd;
cout<<p<<endl<<a<<endl<<b<<endl;
}
调试结果:
C:/Documents and Settings/is54/wjh.cpp(13) : error C2166: l-value specifies const object
(4)
#include<iostream>
using namespace std;
void main()
{ char cc[]="asdf";
char dd[]="jkl";
const char *p=cc;
char const *a=cc;
char * const b=cc;
cout<<p<<endl<<a<<endl<<b<<endl;
p=dd;
a=dd;
cout<<p<<endl<<a<<endl;
}
调试结果:
通过。
运行结果:
asdf
asdf
asdf
jkl
jkl
Press any key to continue
(5)
#include<iostream>
using namespace std;
void main()
{ char cc[]="asdf";
char dd[]="jkl";
const char *p=cc;
char const *a=cc;
char * const b=cc;
cout<<p<<endl<<a<<endl<<b<<endl;
*p='q';
cout<<p<<endl<<a<<endl;
}
调试结果:
C:/Documents and Settings/is54/wjh.cpp(11) : error C2166: l-value specifies const object
(6)
#include<iostream>
using namespace std;
void main()
{ char cc[]="asdf";
char dd[]="jkl";
const char *p=cc;
char const *a=cc;
char * const b=cc;
cout<<p<<endl<<a<<endl<<b<<endl;
*a='q';
cout<<p<<endl<<a<<endl;
}
调试结果:
C:/Documents and Settings/is54/wjh.cpp(11) : error C2166: l-value specifies const object
(7)
#include<iostream>
using namespace std;
void main()
{ char cc[]="asdf";
char dd[]="jkl";
const char *p=cc;
char const *a=cc;
char * const b=cc;
cout<<p<<endl<<a<<endl<<b<<endl;
*b='q';
cout<<b<<endl;
}
调试结果:通过。
运行结果:
asdf
asdf
asdf
qsdf
Press any key to continue
(8)
#include<iostream>
using namespace std;
void main()
{ char cc[]="asdf";
char dd[]="jkl";
const char *p=cc;
cout<<p<<endl;
cc[0]='q';
cout<<p<<endl;
}
调试结果:通过。
运行结果:
asdf
qsdf
Press any key to continue
(9)
#include<iostream>
using namespace std;
void main()
{ char cc[]="asdf";
char dd[]="jkl";
char *const p=cc;
cout<<p<<endl;
cc[0]='q';
cout<<p<<endl;
}
调试结果:通过。
运行结果:
asdf
qsdf
Press any key to continue
(10)
#include<iostream>
using namespace std;
void main()
{ char cc[]="asdf";
char dd[]="jkl";
char const *p=cc;
cout<<p<<endl;
cc[0]='q';
cout<<p<<endl;
}
调试结果:通过。
运行结果:
asdf
qsdf
Press any key to continue
(11)
#include<iostream>
using namespace std;
void main()
{ char cc[]="asdf";
char dd[]="jkl";
const char *p=cc,bb='c';
char const *a=cc;
char * const b=cc;
cout<<p<<endl<<a<<endl<<b<<endl;
bb='d';
}
调试结果:
C:/Program Files/Microsoft Visual Studio/MyProjects/wjh/wjh.cpp(11) : error C2166: l-value specifies const object
分享到:
评论

相关推荐

    const关键字使用总结

    关于C/C++中的const关键字的用法,你了解多少呢?

    C++const关键字详解

    C++中const关键字详解

    C++中const关键字用法详解及实例和源码下载

    现在读到了const关键字的用法,书上面讲解的时候并没有给出完整的实例,只是理论的讲解了一些知识,后来在网上面搜了关于const的关键字的讲解,但是搜到的内容和书上面讲解的内容有着非常让人疑惑的东西,看看下面的...

    c++中const关键字使用详解

    一 const基础;二 const的初始化;三 作为参数和返回值的const修饰符;四 类成员函数中const的使用;五 使用const的一些建议。

    const关键字详解

    关于C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性

    volatile关键字 Const关键字 static关键字 mutable 关键字

    C/C++ 嵌入式 一些关键字: volatile关键字 Const关键字 static关键字 mutable 关键字

    C++中const关键字详解

    C++中const关键字详解

    游戏效率优化(2) 使用const关键字1

    游戏效率优化(2) 使用const关键字1

    C++语言const 关键字使用方法图文详解

    知识用时方恨少,这一段时间正好各种笔试题,其中关于const的用法也是层出不穷,所以疲于在书本上各种翻,这里汇总一下,加深自己的印象的同时,也方便以后查阅和学习。菜鸟一个,若有错误,望指正! const关键字 常...

    详解C语言中const关键字的用法

    关键字const用来定义常量,如果一个变量被const修饰,那么它的值就不能再被改变,我想一定有人有这样的疑问,C语言中不是有#define吗,干嘛还要用const呢,我想事物的存在一定有它自己的道理,所以说const的存在一定...

    浅析c++ 中const关键字

    const是一个C++语言的限定符...2. const作用在编译时,具有类型检查的功能 3. const必须进行初始化 常量指针与指针常量 #include using std::endl; using std::cout; int main() { int a = 100; const int *pa = &

    php中static和const关键字用法分析

    主要介绍了php中static和const关键字用法,结合实例形式分析了static和const关键字的功能、使用方法与相关注意事项,需要的朋友可以参考下

    关键字CONST用法总结

    对于CONST关键字的使用说明及总结,希望对你有用

    总结C语言中const关键字的使用

    const关键字使用非常的灵活,这一点和php差别很大,php中const用来在类中定义一个常量,而在c中,const因位置不同有不同的作用,因情景不同有不同的角色,使用起来也是非常的灵活。 (1):const用来修饰普通的变量...

    关键字const的用法

    介绍了C中关键字const的用法和使用区别

    关键字 const的作用

    绍了关键字 const的作用及举例说明应用的作用

    理解PHP5中static和const关键字的区别

    我们这里对PHP5中的static和const关键字作用进行一下描述,希望对学习PHP5的朋友有帮助。 (1) static static关键字在类中是,描述一个成员是静态的,static能够限制外部的访问,因为static后的成员是属于类的,是不...

    C/C++ 中const关键字的用法小结

    Const作用 NO. 作用 说明 参考 1 可以定义const常量 const int Max = 100; 2 便于进行类型检查 const常量有数据类型,而宏常量没有数据类型。编译器可以对前者进行类型安全检查,而对后者只进行字符替换,...

    关于C的关键字——const的理解和用法

    今天自己写成一篇小总结。如果是初学者,建议好好看一下,相信帮助比较大;如果是高手,请不吝赐教!

Global site tag (gtag.js) - Google Analytics