const 放在函数后表示什么意思 放在函数后面的const是什么意思

const\u5728\u51fd\u6570\u540e\u9762\u662f\u4ec0\u4e48\u610f\u601d\uff1f

const\u5c31\u662f\u8bf4\u8fd9\u4e2a\u51fd\u6570\u64cd\u4f5c\u4e0d\u4f1a\u5bf9\u53d8\u91cf\u6216\u662f\u5bf9\u8c61\u4e4b\u7c7b\u7684\u503c\u6709\u5f71\u54cd \u6bd4\u5982\u4f60\u6709\u4e00\u4e2ahuman\u7c7b \u4f60\u73b0\u5728\u8981\u5f97\u5230\u67d0\u4e2ahuman\u7c7b\u5bf9\u8c61A\u7684age \u90a3\u4e48\u80af\u5b9a\u662f\u4e0d\u4f1a\u56e0\u4e3a\u4f60\u60f3\u5f97\u5230\u8fd9\u4e2a\u503c\u800c\u6539\u53d8\u4e86age\u7684\u5927\u5c0f \u5bf9\u5427 \u90a3\u4e48\u4f60\u5c31\u53ef\u4ee5\u5199\u4e00\u4e2a\u51fd\u6570int getAge\uff08\uff09const\u8fd9\u6837\u5c31\u597d \u8fd9\u4e48\u505a\u662f\u4e3a\u4e86\u9632\u6b62\u4f60\u5728\u51fd\u6570\u4e2d\u5bf9\u4e0d\u5e94\u8be5\u5728\u8fd9\u91cc\u6539\u53d8\u7684\u91cf\u4e0d\u5c0f\u5fc3\u8fdb\u884c\u4e86\u6539\u53d8\u3002\u8fd9\u6837\u7684\u51fd\u6570\u53eb\u5e38\u6210\u5458\u51fd\u6570\u3002\u5e38\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u7406\u89e3\u4e3a\u662f\u4e00\u4e2a\u201c\u53ea\u8bfb\u201d\u51fd\u6570\uff0c\u5b83\u65e2\u4e0d\u80fd\u66f4\u6539\u6570\u636e\u6210\u5458\u7684\u503c\uff0c\u4e5f\u4e0d\u80fd\u8c03\u7528\u90a3\u4e9b\u80fd\u5f15\u8d77\u6570\u636e\u6210\u5458\u503c\u53d8\u5316\u7684\u6210\u5458\u51fd\u6570\uff0c\u53ea\u80fd\u8c03\u7528const\u6210\u5458\u51fd\u6570\u3002
const\u4fee\u9970\u8bf4\u660e\u5b83\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u5b83\u7684\u503c\u662f\u4e0d\u80fd\u88ab\u4fee\u6539\u7684\u3002\u6ca1\u6709\u4ec0\u4e48\u5730\u65b9\u662f\u5fc5\u987b\u8981\u7528\u7684\uff0c\u4f60\u53ea\u8981\u89c9\u5f97\u8fd9\u4e2a\u6570\u5b57\u4f60\u4e0d\u4f1a\u6539\u53d8\u5c31\u53ef\u4ee5\u7528\u3002\u4e00\u822c\u5728\u51fd\u6570\u4f20\u9012\u53c2\u6570\u4e2d\u7528\u7684\u6bd4\u8f83\u591a\uff0c\u4f60\u53ef\u4ee5\u770b\u770bC\u8bed\u8a00\u7684\u5e93\u51fd\u6570\u53c2\u6570\u4e2d\u5f88\u591a\u90fd\u662f\u7528const\u4fee\u9970\u7684\u3002\u6bd4\u5982\u5b57\u7b26\u4e32\u62f7\u8d1d\u7684\u58f0\u660e\u51fd\u6570char *strcpy(char *dest,const char *src); \u540e\u8fb9\u7684\u5f62\u53c2\u7528\u4e86const\u4fee\u9970\uff0c\u5728\u51fd\u6570\u5b9a\u4e49\u4e2dsrc\u7684\u503c\u5c31\u4e0d\u80fd\u88ab\u6539\u53d8\uff0cdest\u7684\u503c\u5c31\u53ef\u4ee5\u88ab\u6539\u53d8\uff0c\u4e5f\u662f\u4e00\u79cd\u63d0\u793a\u7684\u4f5c\u7528\u8bf4\u660e\u5b57\u7b26\u4e32\u662f\u4ecesrc\u62f7\u8d1d\u5230dest\u4e2d

const\u5c31\u662f\u8bf4\u8fd9\u4e2a\u51fd\u6570\u64cd\u4f5c\u4e0d\u4f1a\u5bf9\u53d8\u91cf\u6216\u662f\u5bf9\u8c61\u4e4b\u7c7b\u7684\u503c\u6709\u5f71\u54cd \u6bd4\u5982\u4f60\u6709\u4e00\u4e2ahuman\u7c7b \u4f60\u73b0\u5728\u8981\u5f97\u5230\u67d0\u4e2ahuman\u7c7b\u5bf9\u8c61A\u7684age \u90a3\u4e48\u80af\u5b9a\u662f\u4e0d\u4f1a\u56e0\u4e3a\u4f60\u60f3\u5f97\u5230\u8fd9\u4e2a\u503c\u800c\u6539\u53d8\u4e86age\u7684\u5927\u5c0f \u5bf9\u5427 \u90a3\u4e48\u4f60\u5c31\u53ef\u4ee5\u5199\u4e00\u4e2a\u51fd\u6570int getAge\uff08\uff09const\u8fd9\u6837\u5c31\u597d \u8fd9\u4e48\u505a\u662f\u4e3a\u4e86\u9632\u6b62\u4f60\u5728\u51fd\u6570\u4e2d\u5bf9\u4e0d\u5e94\u8be5\u5728\u8fd9\u91cc\u6539\u53d8\u7684\u91cf\u4e0d\u5c0f\u5fc3\u8fdb\u884c\u4e86\u6539\u53d8\u3002\u8fd9\u6837\u7684\u51fd\u6570\u53eb\u5e38\u6210\u5458\u51fd\u6570\u3002\u5e38\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u7406\u89e3\u4e3a\u662f\u4e00\u4e2a\u201c\u53ea\u8bfb\u201d\u51fd\u6570\uff0c\u5b83\u65e2\u4e0d\u80fd\u66f4\u6539\u6570\u636e\u6210\u5458\u7684\u503c\uff0c\u4e5f\u4e0d\u80fd\u8c03\u7528\u90a3\u4e9b\u80fd\u5f15\u8d77\u6570\u636e\u6210\u5458\u503c\u53d8\u5316\u7684\u6210\u5458\u51fd\u6570\uff0c\u53ea\u80fd\u8c03\u7528const\u6210\u5458\u51fd\u6570\u3002
const\u4fee\u9970\u8bf4\u660e\u5b83\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u5b83\u7684\u503c\u662f\u4e0d\u80fd\u88ab\u4fee\u6539\u7684\u3002\u6ca1\u6709\u4ec0\u4e48\u5730\u65b9\u662f\u5fc5\u987b\u8981\u7528\u7684\uff0c\u4f60\u53ea\u8981\u89c9\u5f97\u8fd9\u4e2a\u6570\u5b57\u4f60\u4e0d\u4f1a\u6539\u53d8\u5c31\u53ef\u4ee5\u7528\u3002\u4e00\u822c\u5728\u51fd\u6570\u4f20\u9012\u53c2\u6570\u4e2d\u7528\u7684\u6bd4\u8f83\u591a\uff0c\u4f60\u53ef\u4ee5\u770b\u770bC\u8bed\u8a00\u7684\u5e93\u51fd\u6570\u53c2\u6570\u4e2d\u5f88\u591a\u90fd\u662f\u7528const\u4fee\u9970\u7684\u3002\u6bd4\u5982\u5b57\u7b26\u4e32\u62f7\u8d1d\u7684\u58f0\u660e\u51fd\u6570char *strcpy(char *dest,const char *src); \u540e\u8fb9\u7684\u5f62\u53c2\u7528\u4e86const\u4fee\u9970\uff0c\u5728\u51fd\u6570\u5b9a\u4e49\u4e2dsrc\u7684\u503c\u5c31\u4e0d\u80fd\u88ab\u6539\u53d8\uff0cdest\u7684\u503c\u5c31\u53ef\u4ee5\u88ab\u6539\u53d8\uff0c\u4e5f\u662f\u4e00\u79cd\u63d0\u793a\u7684\u4f5c\u7528\u8bf4\u660e\u5b57\u7b26\u4e32\u662f\u4ecesrc\u62f7\u8d1d\u5230dest\u4e2d

const 放在函数后表示这个函数是常成员函数, 常成员函数是不能改变成员变量值的函数。
const 限定符,它把一个对象转换成一个常量。
举例:
为了使成员函数的意义更加清楚,我们可在不改变对象的成员函数的函数原型中加上const说明:
class Point
{
public:
int GetX() const;
int GetY() const;
void SetPt (int, int);
void OffsetPt (int, int);
private:
int xVal, yVal;
};
const成员函数应该在函数原型说明和函数定义中都增加const限定:
int Point::GetY() const
{
return yVal;
}
class Set {
public:
Set (void){ card = 0; }
bool Member(const int) const;
void AddElem(const int);

};
bool Set::Member (const int elem) const
{
}
非常量成员函数不能被常量成员对象调用,因为它可能企图修改常量的数据成员:
const Set s;
s.AddElem(10); // 非法: AddElem不是常量成员函数
s.Member(10); // 正确

const 放在函数后表示这个函数是常成员函数, 常成员函数是不能改变成员变量值的函数。

const 限定符,它把一个对象转换成一个常量。

举例:

为了使成员函数的意义更加清楚,我们可在不改变对象的成员函数的函数原型中加上const说明:

class Point

{

public:

int GetX() const;

int GetY() const;

void SetPt (int, int);

void OffsetPt (int, int);

private:

int xVal, yVal;

};

const成员函数应该在函数原型说明和函数定义中都增加const限定:

int Point::GetY() const

{

return yVal;

}

class Set {

public:

Set (void){ card = 0; }

bool Member(const int) const;

void AddElem(const int);

};

bool Set::Member (const int elem) const

{

}

非常量成员函数不能被常量成员对象调用,因为它可能企图修改常量的数据成员:

const Set s;

s.AddElem(10); // 非法: AddElem不是常量成员函数

s.Member(10); // 正确



这样的函数叫常成员函数。常成员函数可以理解为是一个“只读”函数,它既不能更改数据成员的值,也不能调用那些能引起数据成员值变化的成员函数,只能调用const成员函数。

表示函数操作对于类的实例中的成员不进行改变。 这样在传递const A&的参数的时候,你只能调用后面有const的方法。保证不至于因为误操作而改变实例的状态

当const在函数名前面的时候修饰的是函数返回值,在函数名后面表示是常成员函数,该函数不能修改对象内的任何成员,只能发生读操作,不能发生写操作。

扩展阅读:const放在函数后面 ... 高数const什么意思 ... 成员函数后面const ... sin tan cos函数表 ... c函数后面的const作用 ... c++函数后面加const ... consist 是什么意思 ... const在指针前后的区别 ... const放在函数前后的区别 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网