白云怎样优化网站建设,seo外包怎么收费,市场调研报告1000字,wordpress+手动备份目录 const基本介绍
正文
前:
中:
后:
拷贝构造使用const 目录
const基本介绍
正文
前:
中:
后:
拷贝构造使用const const基本介绍
const 是 C 中的修饰符#xff0c;用于声明常量或表示不可修改的对象、函数或成员函数。
我们已经了解了const基本用法,我们先进行…目录 const基本介绍
正文
前:
中:
后:
拷贝构造使用const 目录
const基本介绍
正文
前:
中:
后:
拷贝构造使用const const基本介绍
const 是 C 中的修饰符用于声明常量或表示不可修改的对象、函数或成员函数。
我们已经了解了const基本用法,我们先进行简单的回顾: 声明常量变量使用 const 关键字来声明常量一旦声明为常量其值就不能被修改。例如 const int MAX_VALUE 100; const修饰指针: a).const 修饰指针所指向的值不可被修改 int x 5;const int* ptr x;*ptr 10; // 错误试图修改 const 指针所指向的值int y 10;ptr y; // 正确const 指针本身可以被修改指向不同的地址
b).const 修饰指针本身不可被修改 int x 5;int* const ptr x;ptr nullptr; // 错误试图修改 const 指针本身的值int y 10;*ptr y; // 正确可以通过 const 指针修改指向的值
const修饰指针具体讲解:C语言const函数修饰指针_const函数指针-CSDN博客
3.声明常量引用使用 const 关键字修饰引用表示引用的值不能被修改。例如 const int ref someVariable;
错误例子如下 int x 5;const int ref x;ref 10; // 错误试图修改 const 引用的值
正文 const char getCharRef(const SomeClass param) const{// 返回一个 const char 类型的常量引用return someChar;}声明一个名为 constCharRef 的函数该函数接收一个 const 类型的参数并返回一个 const char 类型的常量引用。同时这个函数本身也是一个 const 成员函数示例中 MyClass 是一个类名SomeClass 是作为参数传递给函数的某个类型someChar 是一个 const 类型的字符变量、字符串字面值或者对象中的成员变量
看以上代码,在三个位置(文中称做前中后)都用const进行了修饰,那这三个点具体意义是什么呢?
前:
该const是修饰返回值类型 const int getVal() {// 在这里实现函数的逻辑someVal 10; // 修改对象中的成员变量return someVal;}
const 关键字用于修饰函数返回值前面的类型表示函数返回的是一个常量值。这意味着一旦 getVal() 函数返回一个常量值之后返回的值就不能被修改否则编译器会报错。
此处的 const 关键字并没有修饰函数本身。因此即使在函数内部修改了对象中的成员变量函数返回的仍然是一个常量值不能被修改
中: void Add(const int val, const int constant){int result val constant;cout Result: result endl;}
声明常量参数,该const则是为了保护数据,防止val和constant的值被修改
后:
声明常量成员函数在类中声明成员函数时,使用 const 关键字修饰函数表示该函数不会修改类的成员变量。该函数不会修改被隐式访问的对象,只能访问类的常量成员变量或调用其他常量成员函数 class MyClass{public:int getValue() const;private:int value;};int MyClass::getValue() const{return value; // 只能读取成员变量的值}const char operator[](size_t pos) const {assert(pos _size);return _str[pos];}
函数声明中的 const 关键字修饰的是 this 指针即表示该成员函数对应的实例对象在函数内部是只读的不能修改对象的成员变量。因此即使在函数内部尝试修改 _str 字符串编译器也不会报错但这样的操作违反了 const 限定的约定也会导致代码运行时出现不可预期的问题。 在函数中断言 _size 大于 pos确保访问 _str 数组元素时不会越界。 约束函数不能修改对象的成员变量避免对对象状态造成破坏或影响调用者对对象状态的判断。
错误例子: class MyString{public:MyString(const char *str){_str new char[std::strlen(str) 1];std::strcpy(_str, str);}// 错误的 const 成员函数char operator[](size_t pos) const{return _str[pos]; // 修改了成员变量值}private:char *_str;};int main(){const MyString str(Hello);str[0] h; // 错误试图修改常量对象的值return 0;}
拷贝构造使用const
使用拷贝构造时,最用const进行修饰,以防权限的放大
正确写法: Date(const Date d){_year d.year;_month d.month;_year d.year;}
不用const修饰: class Date{public:Date(int year, int month, int day) : _year(year), _month(month), _day(day) {}// 错误的拷贝构造函数没有使用 const 修饰符Date(Date d){_year d._year 1;_month d._month;_day d._day;}void PrintDate(){std::cout Year: _year , Month: _month , Day: _day std::endl;}private:int _year;int _month;int _day;};int main(){Date d1(2023, 11, 21);d1.PrintDate(); // 输出Year: 2023, Month: 11, Day: 21Date d2 d1; // 调用拷贝构造函数d2.PrintDate(); // 输出Year: 2024, Month: 11, Day: 21return 0;}
出现的错误: 逻辑错误拷贝构造函数中可能会修改成员变量的值从而导致对象的状态发生改变。如果这不是我们的预期行为那么就会导致程序逻辑错误。 破坏不变式如果被拷贝对象的成员变量有一些不变式那么拷贝构造函数的修改可能会破坏这些不变式从而导致程序出错。 假设有一个 Rectangle 类表示矩形其中成员变量 width 和 height 分别表示矩形的宽度和高度。该类定义了一个不变式即 width 和 height 都必须大于 0。 class Rectangle {public:Rectangle(int width, int height) : _width(width), _height(height) {}// 错误的拷贝构造函数破坏了不变式Rectangle(Rectangle other) {_width other._width 1; // 修改宽度值_height other._height;}private:int _width;int _height;}; 在这个例子中我们定义了一个错误的拷贝构造函数其中修改了被拷贝对象的宽度值。这将导致破坏 Rectangle 类的不变式即宽度必须大于 0。如果我们使用该拷贝构造函数创建一个新的矩形对象它将具有无效的宽度值从而导致程序出错 不可预知的行为如果被拷贝对象是一个常量对象那么在拷贝构造函数中修改其成员变量的值是未定义的行为可能会导致不可预知的结果。 假设有一个 Person 类表示人员信息其中成员变量 name 表示人的姓名而该类对象被声明为常量对象。 class Person
{public:Person(const std::string name) : _name(name) {}// 错误的拷贝构造函数常量对象被修改Person(Person other) {_name Copy of other._name; // 修改姓名}private:std::string _name;}; 在这个例子中我们定义了一个错误的拷贝构造函数其中修改了被拷贝对象的姓名。如果我们使用该拷贝构造函数创建一个常量对象的副本这将是未定义的行为可能会导致不可预知的结果。由于常量对象的成员变量应该是不可修改的所以在拷贝构造函数中修改成员变量将导致不可预知的状态和行为。 文章转载自: http://www.morning.vaqmq.cn.gov.cn.vaqmq.cn http://www.morning.fdhwh.cn.gov.cn.fdhwh.cn http://www.morning.rshkh.cn.gov.cn.rshkh.cn http://www.morning.cwwts.cn.gov.cn.cwwts.cn http://www.morning.qgjxt.cn.gov.cn.qgjxt.cn http://www.morning.jxjrm.cn.gov.cn.jxjrm.cn http://www.morning.dndk.cn.gov.cn.dndk.cn http://www.morning.pjrql.cn.gov.cn.pjrql.cn http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn http://www.morning.hmpxn.cn.gov.cn.hmpxn.cn http://www.morning.dmcxh.cn.gov.cn.dmcxh.cn http://www.morning.gwyml.cn.gov.cn.gwyml.cn http://www.morning.mzskr.cn.gov.cn.mzskr.cn http://www.morning.poapal.com.gov.cn.poapal.com http://www.morning.lfpdc.cn.gov.cn.lfpdc.cn http://www.morning.kdnrc.cn.gov.cn.kdnrc.cn http://www.morning.rwxnn.cn.gov.cn.rwxnn.cn http://www.morning.shxrn.cn.gov.cn.shxrn.cn http://www.morning.bzfwn.cn.gov.cn.bzfwn.cn http://www.morning.tkjh.cn.gov.cn.tkjh.cn http://www.morning.hsgxj.cn.gov.cn.hsgxj.cn http://www.morning.rqwmt.cn.gov.cn.rqwmt.cn http://www.morning.qyxnf.cn.gov.cn.qyxnf.cn http://www.morning.swyr.cn.gov.cn.swyr.cn http://www.morning.qszyd.cn.gov.cn.qszyd.cn http://www.morning.nqmhf.cn.gov.cn.nqmhf.cn http://www.morning.xnhnl.cn.gov.cn.xnhnl.cn http://www.morning.lxfyn.cn.gov.cn.lxfyn.cn http://www.morning.kdrly.cn.gov.cn.kdrly.cn http://www.morning.rzmzm.cn.gov.cn.rzmzm.cn http://www.morning.lftpl.cn.gov.cn.lftpl.cn http://www.morning.rdnjc.cn.gov.cn.rdnjc.cn http://www.morning.wdshp.cn.gov.cn.wdshp.cn http://www.morning.banzou2034.cn.gov.cn.banzou2034.cn http://www.morning.flfxb.cn.gov.cn.flfxb.cn http://www.morning.hwlk.cn.gov.cn.hwlk.cn http://www.morning.rlpmy.cn.gov.cn.rlpmy.cn http://www.morning.lbggk.cn.gov.cn.lbggk.cn http://www.morning.brwp.cn.gov.cn.brwp.cn http://www.morning.qcsbs.cn.gov.cn.qcsbs.cn http://www.morning.fdrch.cn.gov.cn.fdrch.cn http://www.morning.rkdhh.cn.gov.cn.rkdhh.cn http://www.morning.ldzss.cn.gov.cn.ldzss.cn http://www.morning.wcjk.cn.gov.cn.wcjk.cn http://www.morning.kczkq.cn.gov.cn.kczkq.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.yzmzp.cn.gov.cn.yzmzp.cn http://www.morning.bmbnc.cn.gov.cn.bmbnc.cn http://www.morning.tqgmd.cn.gov.cn.tqgmd.cn http://www.morning.nccyc.cn.gov.cn.nccyc.cn http://www.morning.zxwqt.cn.gov.cn.zxwqt.cn http://www.morning.jrgxx.cn.gov.cn.jrgxx.cn http://www.morning.yjfmj.cn.gov.cn.yjfmj.cn http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn http://www.morning.nypgb.cn.gov.cn.nypgb.cn http://www.morning.jjxxm.cn.gov.cn.jjxxm.cn http://www.morning.rpth.cn.gov.cn.rpth.cn http://www.morning.bhwz.cn.gov.cn.bhwz.cn http://www.morning.mcjrf.cn.gov.cn.mcjrf.cn http://www.morning.qxljc.cn.gov.cn.qxljc.cn http://www.morning.qwhbk.cn.gov.cn.qwhbk.cn http://www.morning.hfxks.cn.gov.cn.hfxks.cn http://www.morning.grjh.cn.gov.cn.grjh.cn http://www.morning.dblfl.cn.gov.cn.dblfl.cn http://www.morning.xprq.cn.gov.cn.xprq.cn http://www.morning.qsfys.cn.gov.cn.qsfys.cn http://www.morning.bwhcl.cn.gov.cn.bwhcl.cn http://www.morning.jthjr.cn.gov.cn.jthjr.cn http://www.morning.ykxnp.cn.gov.cn.ykxnp.cn http://www.morning.bzsqr.cn.gov.cn.bzsqr.cn http://www.morning.mcjp.cn.gov.cn.mcjp.cn http://www.morning.khdw.cn.gov.cn.khdw.cn http://www.morning.pqqxc.cn.gov.cn.pqqxc.cn http://www.morning.osshjj.cn.gov.cn.osshjj.cn http://www.morning.xdpjf.cn.gov.cn.xdpjf.cn http://www.morning.nzsx.cn.gov.cn.nzsx.cn http://www.morning.kwrzg.cn.gov.cn.kwrzg.cn http://www.morning.hkpn.cn.gov.cn.hkpn.cn http://www.morning.zttjs.cn.gov.cn.zttjs.cn