constの使い方について

4763 ワード


<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 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<  //const int u = 8;//エラー、定数を通常変数で参照しようとすると、通常変数で修正できることを意味するので、コンパイルは//int&c=uではできません.   //cout<< c <    cout<<"process is runing !"<    p_string = str;     *(p_string +1)  = 'P';//constが指すオブジェクトの値はcout</* 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 > 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; }*/
/**通常、実パラメータの値を変更しない場合は、void fun(const int i)をconstで限定する.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; } */