关于const用法小结



<span style="font-size:24px;">#include<iostream>
using std ::cout;
using std ::string;
using std ::cin;
using std ::endl;

int main (int argc, char **argv)
{
   // const int i;    //错误:常量必须初始化
    int b = 9;
   // const int i = 8;     //正确:可以用字面值常量进行初始化
    //const int i = b;       //正确:常变量可以用普通变量进行初始化
    const int *des = 0;
    const int *p_int = 0;
    des = p_int;
    des = &b;
  //  *des = 9;       //错误 const int * 代表指针所指向的空间的值不能进行修改,但指针的指向可以进行改变

   int i = 9;
    int const  &n = i;    //正确 常量可以引用普通变量
   // n = 10;             错误,n 不可以改变,only read
    cout << n <<endl;

   // const int u = 8;    //错误,试图用普通变量引用常量,就意味着可以通过普通变量进行修改,所以编译不能通过  
   // int &c  = u ;
    //cout<< c <<endl;

    cout<<"process is runing !"<<endl;
    return 0;
}
/*
void my_printf(const char *str);
void my_printf(const char *str)
{
   // char *p_string = 0;      //不能将常量的值给普通变量,因为p_string可能会修改常量的值 
    const char * p_string = 0;

    p_string = str;
    *(p_string +1)  = 'P';      //这是错误的,因为const 所指向的对象的值不能改变
    cout <<str<<endl;
    cout << "runing is successful"<<endl;
    return ;
}
int main (int argc,char **argv)
{
    char str1[] = "helllo world!";
    my_printf(str1);
    return 0;
}*/

/*
bool is_short(const string &s1,const string &s2);
bool is_short(const string &s1,const string &s2)
{
    
     if (s1.size() < s2.size()){
         cout << "s1 <s2" << endl;
     }
int main (int argc, char **argv)
{
    string s1;
    string s2;
    cin >> s1;
    cin >> s2;
    is_short(s1,s2);
    return 0;
}*/
/*               C++ 与 C语言的区别
 *               C语言要求函数名不能重复
 *               而C++可以函数名相同,只不过参数列表要不同
void fun(const int i);
void fun(int i);
void fun(const int i)
{
    return ;
}
void fun (int i,int j)
{
    i += j;
    cout << i << endl;
    return ;
}
int main (int argc,char ** argv)
{
    int i,j;
    i = 1;
    j = 3;
    fun(i , j);
    return 0;
}*/

/*
 *      一般当当不改变实参的值的的时候,就用const来进行限定
void fun(const int i);
void fun (const int i)
{
    int j = 0;
    j = i;
    cout << j << endl;
    return ;
}
int main (int argc,char ** argv)
{

   const  int  des = 9;
   int fin = 0;
   fin = des;
    fun(fin);
    return 0;
}
*/
</span>



#include<iostream>
using std ::cout;
using std ::string;
using std ::cin;
using std ::endl;

int main (int argc, char **argv)
{
   // const int i;    //错误:常量必须初始化
    int b = 9;
   // const int i = 8;     //正确:可以用字面值常量进行初始化
    //const int i = b;       //正确:常变量可以用普通变量进行初始化
    const int *des = 0;
    const int *p_int = 0;
    des = p_int;
    des = &b;
  //  *des = 9;       //错误 const int * 代表指针所指向的空间的值不能进行修改,但指针的指向可以进行改变

   int i = 9;
    int const  &n = i;    //正确 常量可以引用普通变量
   // n = 10;             错误,n 不可以改变,only read
    cout << n <<endl;

   // const int u = 8;    //错误,试图用普通变量引用常量,就意味着可以通过普通变量进行修改,所以编译不能通过 
   // int &c  = u ;
    //cout<< c <<endl;

    cout<<"process is runing !"<<endl;
    return 0;
}
/*
void my_printf(const char *str);
void my_printf(const char *str)
{
   // char *p_string = 0;      //不能将常量的值给普通变量,因为p_string可能会修改常量的值
    const char * p_string = 0;

    p_string = str;
    *(p_string +1)  = 'P';      //这是错误的,因为const 所指向的对象的值不能改变
    cout <<str<<endl;
    cout << "runing is successful"<<endl;
    return ;
}
int main (int argc,char **argv)
{
    char str1[] = "helllo world!";
    my_printf(str1);
    return 0;
}*/

/*
bool is_short(const string &s1,const string &s2);
bool is_short(const string &s1,const string &s2)
{
   
     if (s1.size() < s2.size()){
         cout << "s1 <s2" << endl;
     }
int main (int argc, char **argv)
{
    string s1;
    string s2;
    cin >> s1;
    cin >> s2;
    is_short(s1,s2);
    return 0;
}*/
/*               C++ 与 C语言的区别
 *               C语言要求函数名不能重复
 *               而C++可以函数名相同,只不过参数列表要不同
void fun(const int i);
void fun(int i);
void fun(const int i)
{
    return ;
}
void fun (int i,int j)
{
    i += j;
    cout << i << endl;
    return ;
}
int main (int argc,char ** argv)
{
    int i,j;
    i = 1;
    j = 3;
    fun(i , j);
    return 0;
}*/

/*
 *      一般当当不改变实参的值的的时候,就用const来进行限定
void fun(const int i);
void fun (const int i)
{
    int j = 0;
    j = i;
    cout << j << endl;
    return ;
}
int main (int argc,char ** argv)
{

   const  int  des = 9;
   int fin = 0;
   fin = des;
    fun(fin);
    return 0;
}
*/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值