参数前的const vs 函数名后的const c++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15999123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
const before parameter vs const after function name c++
提问by jazzybazz
What is the difference betweeen something like this
像这样的东西有什么区别
friend Circle copy(const Circle &);
and something like this
和这样的事情
friend Circle copy(Circle&) const;
I know const after the function is used to tell the compiler that this function won't attempt to change the object it is called on, what about the other one?
我知道 const 在函数被用来告诉编译器这个函数不会试图改变它被调用的对象之后,另一个呢?
回答by Andy Prowl
The first form means that the (state of the) Circle
object bound to the reference which is the parameter of the copy()
function will not be altered by copy()
through that reference. The reference is a reference to const
, so it won't be possible to invoke member functions of Circle
through that reference which are not themselves qualified as const
.
第一种形式意味着Circle
绑定到作为copy()
函数参数的引用的对象的(状态)不会copy()
通过该引用改变。该引用是对 的引用const
,因此不可能Circle
通过该引用调用 的成员函数,这些函数本身没有被限定为const
。
The second form, on the other hand, is illegal: only member functionscan be const
-qualified (while what you are declaring there is a global, friend
function).
另一方面,第二种形式是非法的:只有成员函数可以被const
限定(而你声明的是一个全局friend
函数)。
When const
qualifies a member function, the qualification refers to the implicit this
argument. In other words, that function will not be allowed to alter the state of the object it is invoked on (the object pointed to by the implicit this
pointer) - with the exception of mutable
objects, but that's another story.
当const
限定成员函数时,限定指的是隐式this
参数。换句话说,该函数将不允许改变它被调用的对象(隐式this
指针指向的mutable
对象)的状态——对象除外,但那是另一回事了。
To say it with code:
用代码说:
struct X
{
void foo() const // <== The implicit "this" pointer is const-qualified!
{
_x = 42; // ERROR! The "this" pointer is implicitly const
_y = 42; // OK (_y is mutable)
}
void bar(X& obj) const // <== The implicit "this" pointer is const-qualified!
{
obj._x = 42; // OK! obj is a reference to non-const
_x = 42; // ERROR! The "this" pointer is implicitly const
}
void bar(X const& obj) // <== The implicit "this" pointer is NOT const-qualified!
{
obj._x = 42; // ERROR! obj is a reference to const
obj._y = 42; // OK! obj is a reference to const, but _y is mutable
_x = 42; // OK! The "this" pointer is implicitly non-const
}
int _x;
mutable int _y;
};
回答by John Zwinck
C++ class methods have an implicit this
parameter which comes before all the explicit ones. So a function declared within a class like this:
C++ 类方法有一个隐式this
参数,它出现在所有显式方法之前。所以在类中声明的函数是这样的:
class C {
void f(int x);
You can imagine really looks like this:
你可以想象真的是这样的:
void f(C* this, int x);
Now, if you declare it this way:
现在,如果你这样声明:
void f(int x) const;
It's as if you wrote this:
就好像你写了这个:
void f(const C* this, int x);
That is, the trailing const
makes the this
parameter const, meaning that you can invoke the method on const objects of the class type, and that the method cannot modify the object on which it was invoked (at least, not via the normal channels).
也就是说,尾随const
使this
参数成为 const,这意味着您可以在类类型的 const 对象上调用该方法,并且该方法不能修改调用它的对象(至少,不能通过正常通道)。
回答by Waqar
Circle copy(Circle&) const;
makes the function const
itself. This can only be used for member functions of a class/struct.
使函数const
本身。这只能用于类/结构的成员函数。
Making a member function const
means that
制作成员函数const
意味着
- it cannot call any non-const member functions
- it cannot change any member variables.
- it can be called by a const object(
const
objects can only callconst
functions). Non-const objects can also call aconst
function.
- 它不能调用任何非常量成员函数
- 它不能改变任何成员变量。
- 它可以被一个 const 对象
const
调用(对象只能调用const
函数)。非常量对象也可以调用const
函数。
This one must be member function of the class 'Circle'.
这个必须是类' Circle'的成员函数。
Now consider the next one:
现在考虑下一个:
Circle copy(const Circle &);
while this one means that the parameter passed cannot be changed within the function. This one may or may not be a member function of the class.
而这意味着传递的参数不能在函数内更改。这个可能是也可能不是类的成员函数。
NOTE:It is possible to overload a function in such a way to have a const
and non-const version of the same function.
注意:可以以具有const
相同函数的非常量版本和非常量版本的方式重载函数。
回答by Alex Chamberlain
One refers to the parameter the other to the function.
一个引用参数,另一个引用函数。
Circle copy(const Circle &);
This means that the parameter passed in cannot be changed within the function
这意味着传入的参数不能在函数内更改
Circle copy(Circle&) const;
The const
qualified function is used for member functions and means you cannot change the data members of the object itself. The example you posted was nonsensical.
的const
合格功能用于成员函数和方法不能更改对象本身的数据成员。您发布的示例是荒谬的。
Read right-to-left
从右到左阅读
If we rewrite the first function as Circle copy(Circle const&);
, which means the same thing, it becomes clear that reading right to left becomes useful. copy
is a function that takes a const
reference to a Circle
object and returns a Circle
object by reference.
如果我们将第一个函数重写为Circle copy(Circle const&);
,这意味着同样的事情,很明显从右到左阅读变得有用。copy
是,需要一个函数const
到参考Circle
对象,并返回一个Circle
通过引用对象。
回答by shivakumar
friend Circle copy(const Circle &);
//refers to constant parameter of the function. cant' change the value stored by parameter.
friend Circle copy(const Circle &);
//指的是函数的常量参数。不能'改变参数存储的值。
Need to remove friend in your example Circle copy(Circle&) const;//can't change this poniter value named as Constant member function
需要在您的示例中删除朋友 Circle copy(Circle&) const; //不能改变这个命名为常量成员函数的指针值
回答by Sam
friend Circle copy(const Circle &);
The value of parameter will not be changed during the function calls.
参数值在函数调用过程中不会改变。
friend Circle copy(const Circle &)const ;
The function is an accessor that does not change any value of class members. Generally, there are to types of functions: accessors and mutators. Accessor: examines but does not change the state of its object.
该函数是一个不会改变任何类成员值的访问器。通常,函数有以下类型:访问器和修改器。访问器:检查但不改变其对象的状态。