关于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