zoukankan      html  css  js  c++  java
  • 共享数据的包含const

    1、常引用:被引用的对象不能被更新 使用:const 类型名 &引用对象    如const int &a;

    2、常对象:必须进行初始化,并且对象不能改变    使用:类名 const 对象名  常对象只能访问常成员函数;

    3、常数组:数组元素不能改变 使用:const 数组名

    4、常指针:指向常量的指针 const char *p

    5、常成员函数:在函数体中不能改变对象的数据成员;使用:void A::f() const{}

    注意:const可以参与对重载函数的区分;

    常引用做形参:

    #include<iostream>
    using namespace std;
    void display(const double& r);
    int main()
    {   double d(9.5);
         display(d);
         return 0;
    }
    void display(const double& r)
    //常引用作形参,在函数中不能更新 r所引用的对象。
    {   cout<<r<<endl;   }

    常对象:

    class A
    {
      public:
        A(int i,int j) {x=i; y=j;}
                         ...
      private:
        int x,y;
    };
    A const a(3,4); //a是常对象,不能被更新

    常成员函数:

    #include<iostream>
    using namespace std;
    class Cuboid
    {
        const int length;
        const int weight;
        const int height;
        
        public:
            Cuboid(int i,int k,int j);
             int volume() const;//常成员函数,,,,常对象只能访问常成员函数;
    
    };
    Cuboid::Cuboid(int i,int k,int j):length(i),weight(j),height(k)
    {}
    
    int Cuboid::volume() const
    {
    return length*weight*height;
    }
    int main()
    {
    Cuboid const cu(12,10,12);
    cout<<"volume = "<<cu.volume()<<"
    ";
    
    }
  • 相关阅读:
    runlevel=$(set -- $(runlevel); eval "echo $$#" )
    MPLS
    sql server 查询存储过程返回值
    sql 游标的关闭和释放
    sql 查询某一列最大的数据
    flex label如何通过AS3实现颜色设置
    sql server 字符串拆分
    Linux centos 解决"不在 sudoers 文件中。此事将被报告"的问题
    Flex String拼接
    flex 判断对象的类型
  • 原文地址:https://www.cnblogs.com/defen/p/5312548.html
Copyright © 2011-2022 走看看