简体   繁体   English

int const函数(参数),int函数(const参数)和int函数(参数)const之间有什么区别?

[英]What is the difference between int const function(parameters), int function(const parameters), and int function(parameters) const?

I'm working on an assignment for my CS class and it uses const, and I am kind of confused as to when to use each. 我正在为我的CS类工作,它使用const,我对何时使用它们感到困惑。

What are the differences between these 3 functions? 这3个功能有什么区别?

 int const function(parameters)
 int function(const parameters)
 int function(parameters) const

Thanks in advance! 提前致谢!

int const function(parameters)

The const is redundant. const是多余的。 Declaring a simple type such as int as a const return value is not useful, although it may help to make the code more self-documenting. int这样的简单类型声明为const返回值是没有用的,尽管它可能有助于使代码更加自我记录。 If the return type is a reference or pointer however then the story changes. 如果返回类型是引用或指针,则故事会发生变化。

int function(const parameters)

The parameters passed to function are const and so can not be modified. 传递给functionparametersconst ,因此无法修改。

int function(parameters) const

function is a method which does not modify any of the member variables in the object instance for which it is being invoked. function是一种方法 ,它不会修改被调用它的对象实例中的任何成员变量。

Considering you talking about c++ : 考虑到你在谈论c++

const int function(parameters) // instead of your  int const function(parameters)

will return a constant to the int 将返回一个常量到int

int function(const parameters)

The parameters will be considered as constant inside the method, which means they will not be changed. 方法内的参数将被视为常量,这意味着它们不会被更改。

int function(parameters) const

This function will not change any class variable (if the member is not mutable) 此函数不会更改任何类变量(如果成员不可变)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM