c++ 五十行看懂const用法

为了编程程序的使用和变量的维护,同时很多返回数值为了避免频繁的数据复制,经常会使用const关键字。下面对关键字的几个用法进行测试和总结:

这绝对是最简单最实用的const讲解! 我所需要的const都在这里。

#include <termio.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <set>
#include <cmath>
class TestConst {
 public:
  TestConst (int index) : private_index_(index) {

  }
  //第一个const int& 表示返回私有变量的指针。 第二个const表示不允许改变内部的数值。
  //但是返回值会随着实例化对象的生命周期消失而消失的。
  const int& private_index() const {   
    return private_index_;
  }
  //通过赋值进行私有变量的获取,将对该变量进行拷贝。 const表示不允许改变内部的数值
  int private_index_assign() const {
    return private_index_;
  }

  //返回私有变量的指针,这里就不能够使用const,因为返回了指针,
  //int* mutable_index() const   //错误的写法,返回了指针就意味着改变,不能使用const。
  int* mutable_index() {
    return &private_index_;
  }
  //赋值设置
  void ste_private_index(const int& index) {
    //index++; error!! 因为输入的index类型是const的,所以不能够改变。
    //采取这种方式输入的好处是即采用了引用输入,避免了一个复制,又保证了输入的变量不被函数内部所改变。
    private_index_ = index;
  }
  //显示这个变量的内存地址
  void ShowIndexAddress() const {
    std::cout << "  private_index_ address : " << &private_index_ << std::endl;
  }

 private:
  int private_index_ ;
};
//**错误示范** 返回了一个局部变量的地址,虽然编译不会报错。但是在运行返回一个被释放的地址是错误的!
//正确的应该是返回int, 不能够为地址!
const int& Added(const int& input) {
  int output = input;
  output = output + 10;
  return output;    //run error!!
}
//这里的const修饰的是指针,表示指针地址不可变,但是内容是可变的。
void AddedInput(int* const input) {
  *input = *input + 10;   //内容变化, 正确!
  int other_address = 10;
  //input = &other_address;  //指向的地址变化, 编译error!!
  
}

int main(int argc, char *argv[]) {
  TestConst test_class(1);
  {
    //test const。 返回了私有变量的地址,但是不能够改变这个地址和地址指向的内容
    std::cout << "Test const & : " << std::endl;
    const int& index =
        test_class.private_index();
    //index = index - 1;       //e错误! 不能够改变数值
    std::cout << "  index address : " << &index << std::endl;
    test_class.ShowIndexAddress();
  }

  {
    //test !const, 返回了私有变量的地址,但是将其复制给了index变量,所以index可改变, 
    std::cout << "Test !const & : " << std::endl;
    int index =
        test_class.private_index();
    //index = index - 1;   //正确
    std::cout << "  index address : " << &index << std::endl;
    test_class.ShowIndexAddress();
  }

  {
    //test assign,  直接通过拷贝返回数值
    std::cout << "Test assign  : " << std::endl;
    int index =
        test_class.private_index_assign();
    //index = index - 1;
    std::cout << "  index address : " << &index << std::endl;
    test_class.ShowIndexAddress();
  }

  {
    //test mutable  返回私有变量的指针
    std::cout << "Test mutable & : " << std::endl;
    int* index = test_class.mutable_index();
    *index = *index - 1;          //同时会改变private_index_的数值
    std::cout << "  index address : " << index << std::endl;
    test_class.ShowIndexAddress();
    std::cout << test_class.private_index() << std::endl;
  }

  {
    //测试指针输入
    int test_origin = 10;
    AddedInput(&test_origin);   //对数值增加了10
    std::cout << "test_origin: " << test_origin << std::endl; 

  }
  
  {
    //test const return local variable
    int test = Added(10);
    std::cout << "  test: " << &test << std::endl;  //will run error here!
  }

  return 0;
}

程序输出为:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值