C++ 笔记(const)

date
Mar 26, 2021
slug
cpp-notes-const
status
Published
tags
c++
summary
type
Post
读书笔记

修饰成员变量

#include<iostream>
using namespace std;
int main(){
    int a1=10;   // 非常量
    const int a2=a1;    // 常量
    int* a3 = &a1;   // 非 const 的指针
    const int* a4 = &a1;   // 指针可以变,但是数据不能变
    int const *m = &a1;    // 和上面一样
    int* const a5 = &a1;   // 指针不可变,数据可以变
    int const * const a6 = &a1;   // 数据和指针都不能变
    const int * c修饰成员变量onst a7 = &a1;   // 同上
    return 0;
}
主要的难点在于 const 的位置,比较灵活,记住这三点:
  1. 一个 const,如果 const 在 * 前面, 表示指针所指 数据是常量 ,不能通过解引用修改该数据; 指针本身是变量 ,可以指向其他的内存单元。
  1. 一个 const,如果 const 在 * 后面,表示 指针本身是常量 ,不能指向其他内存地址;指针所指的 数据可以通过解引用修改
  1. 两个 const,* 左右各一个,表示指针和指针所指数据都不能修改。

修饰函数参数

void hello(const int x)
{
    x=5;// 编译出错
}
这个很好理解,传递过来的参数不可修改。

修饰成员返回值

分两种返回值,指针传递或者值传递。
  1. 指针传递
const int* hello()
{
    int*a = new int(10);
    return a;
}
int main()
{
    const int* a = hello();
    int* b = hello();// 编译出错
    return 0;
}
按照修饰成员变量的判断法则,const 在 * 前面,所以指针是变量,值是常量。
  1. 值传递 对于值传递来说,函数会把返回值复制到外部的临时存储单元中,加了 const 修饰没有任何价值。所以 对于值传递来说,加 const 没有太多意义

修饰成员函数

#include <iostream>
using namespace std;
class Point{
    int x;
    public :
    Point(int _x):x(_x){}
    void hello(int _x) const
    {
        x=_x;// 错误,在 const 成员函数中,不能修改任何类成员变量
        modify_x(_x);// 错误,const 成员函数不能调用非 onst 成员函数,因为非 const 成员函数可以会修改成员变量
    }
    void modify_x(int _x)
    {
        x=_x;
    }
};
  1. const 修饰的成员函数不能修改任何的成员变量 (mutable 修饰的变量除外 )
  1. const 成员函数不能调用非 const 成员函数,因为非 const 成员函数可以会修改成员变量

const 强制转化

const_cast 是用来移除变量的 const 或 volatile 限定符
#include <iostream>
using namespace std;
int main()
{
    const int constant = 21;
    const int *const_p = &constant;
    int *modifier = const_cast<int *>(const_p);
    *modifier = 7;
    cout << "constant:" << constant << endl; //constant: 21
    cout << "const_p:" << *const_p << endl; //const_p: 7
    cout << "modifier:" << *modifier << endl; //modifier: 7
}
constant 还是原来的值,但是 const_p 的值变了,因为指针可以修改,但是 const_p 的值不能变,所以 const_p 和 modifier 指向了同一个地址,所以他俩的值一样。平时不会这么用,我认为用的比较多的是在参数传递上。
#include <iostream>
#include <string>
using namespace std;
// 这是一个底层的接口,没办法对这个接口进行修改
const string &hello(const string &a1, const string &a2)
{
    return (a1.size() < a2.size()) ? a1 : a2;
}
int main()
{
    string a1 = "123", a2 = "1111111";
    //string& result = hello(a1,a2) 由于定义的是 const 返回值,参数也是 const,所以这样写肯定报错。
    string &result = const_cast<string &>(hello(const_cast<const string &>(a1), const_cast<const string &>(a2))); // 这样就可以了
    cout << result << endl;
}

© Frandy 2024