zoukankan      html  css  js  c++  java
  • C++

    对于常类型 有以下几点 要注意:

     首先可归为 : 常数据成员 const 类型  元素;

                       

     #include <iostream>
    using namespace std;
    class S
    {
    public :
    
        S(int i, int j):m(i),n(j)//只能通过构造函数的成员初始化列表对常数据成员进行初始化
        {
    
        }
    
        void display()
        {
            cout<<"m = "<<m<<endl;
            cout<<"n = " << n<<endl;
        }
    private :
        const int m ;
        const int n ;
    };
    int main()
    {
       S s(1,2);
       s.display();
        return 0;
    }

     常对象       const  类名 对象名【参数列表】;

      

    #include <iostream>
    using namespace std;
    class S
    {
    public :
    
        S(int i, int j)
        {
            m = i;
            n = j;
        }
    
        void display()
        {
            cout<<"m = "<<m<<endl;
            cout<<"n = " << n<<endl;
        }
    private :
         int m , n;
    };
    int main()
    {
       S const s(1,2);
       ///s.display();禁止的 不允许 常对象 调用 普通成员函数
        return 0;
    }

      常函数       数据类型  函数名【参数列表】const;

    #include <iostream>
    using namespace std;
    class S
    {
    public :
    
        S(int i, int j)
        {
            m = i;
            n = j;
        }
    
        void  setvalue( ) const
        {
         cout<<" this is const 函数"<<endl;
        }
    
        void display() const
        {
            cout<<"m = "<<m<<endl;
            cout<<"n = " << n<<endl;
        }
    private :
         int m , n;
    };
    int main()
    {
       S const s(1,2);
       s.setvalue();
       s.display();
        return 0;

    常引用       const 数据类型   &引用名=目标变量名; 

       例1
    int a = 1 ;
    const int &ra=a;
     当 ra=4;   a = 4;

    对引用变量的改变就是对原变量的改变

     常对象的数据成员值在在对象整个生存期中是不能改变的;

  • 相关阅读:
    Linux DNS配置
    SqlCommandBuilder 为SqlDataAdapter 提供 update、insert、delete 命令(自己利用select命令自动从数据库元数据推断)
    Dev中GridView——事件
    select 动态赋值 layui重新渲染
    url-loader和file-loader的区别和使用
    Fetch API
    Fetch API
    FormData上传文件
    Buffer、ArrayBuffer互转
    if exists用法(判断对象的用法)
  • 原文地址:https://www.cnblogs.com/webph/p/5544160.html
Copyright © 2011-2022 走看看