参数之前的 const 与函数名 c + + 之后的 const

像这样的事情有什么区别

friend Circle copy(const Circle &);

还有这样的东西

friend Circle copy(Circle&) const;

我知道 const 函数是用来告诉编译器这个函数不会尝试改变它被调用的对象,那么另一个函数呢?

96655 次浏览

一个引用参数,另一个引用函数。

Circle copy(const Circle &);

这意味着不能在函数中更改传入的参数

Circle copy(Circle&) const;

const限定函数用于成员函数,意味着不能更改对象本身的数据成员。你发布的例子毫无意义。

从右往左读

如果我们将第一个函数重写为 Circle copy(Circle const&);,这意味着同样的事情,那么从右向左读就变得非常有用了。ABC1是一个函数,它接受对 Circle对象的 ABC2引用,并通过引用返回 Circle对象。

第一种形式意味着绑定到作为 copy()函数参数的引用的 Circle对象的(状态)不会被 copy()通过该引用改变。该引用是对 const的引用,因此不可能通过该引用调用 Circle的成员函数,因为该引用本身并不限定为 const

另一方面,第二种形式是非法的: 只有 成员函数成员函数可以是 const限定的(而您声明的是一个全局的 friend函数)。

const限定一个成员函数时,该限定引用隐式 this参数。换句话说,除了 mutable对象之外,这个函数不允许改变被调用对象的状态(隐式 this指针指向的对象) ,但这是另一回事。

用暗语说:

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;
};

friend Circle copy(const Circle &);//指的是函数的常量参数。不能改变参数存储的值。

需要删除朋友在你的例子 圈复制(圈 &)常量; //不能更改名为 Constant 成员函数的 Poniter 值

C + + 类方法有一个隐式的 this参数,它位于所有显式参数之前。在类中声明的函数如下:

class C {
void f(int x);

你可以想象它看起来是这样的:

  void f(C* this, int x);

现在,如果你这样声明:

  void f(int x) const;

就好像是你写的:

  void f(const C* this, int x);

也就是说,后面的 const创建了 this参数 const,这意味着您可以对类类型的 const 对象调用该方法,而且该方法不能修改调用它的对象(至少不能通过普通通道)。

Circle copy(Circle&) const;

只能用于 class/struct 的成员函数。

使成员函数 const意味着

  • 不能调用任何非常数成员函数
  • 不能改变任何成员变量。
  • 它可以由 const对象调用(const对象只能调用 const函数)。非常量对象也可以调用 const函数。
  • 它是类“ 转圈”的成员函数。

现在考虑下一个问题:

Circle copy(const Circle &);

而这意味着不能在函数中更改传递的参数。它可能是类的成员函数,也可能不是。

注意: 以这样的方式重载一个函数是可能的,即同一个函数有一个 const和非常量版本。

friend Circle copy(const Circle &);

在函数调用期间,不会更改参数的值。

friend Circle copy(const Circle &)const ;

该函数是一个不更改类成员任何值的访问器。一般来说,函数的类型有: 访问器和变异器。 访问器: 检查但不更改其对象的状态。


让我们清除所有与 < strong > const有关的混淆


const 来自于 不变,意思是某些东西不能改变但是可读。

  1. 如果我们用 const关键字限定变量,以后就不能更改它了。
    例如:。
    const int var =25; const 变量在声明时必须初始化。
    var =50; // gives error

  2. 如果我们用 const 之后 *限定我们的指针变量,那么我们 不行改变指针本身,但指针的内容是 变化无常
    例如:。
    int *const ptr = new int;
    ptr = new int; //gives error
    但是
    *ptr=5445; //allowed

  3. 如果我们用 const 之前 *限定我们的指针变量,那么我们 可以改变指针本身,但指针的内容是 不可改变
    例如:。
    intconst* ptr = new int(85);
    //or
    const int * ptr = new int(85);
    ptr = new int; // allowed
    但是
    *ptr=5445; // gives error < br/>

  4. 指针和内容都是常量
    例如:。
    intconst*constptr = new int(85);
    //or
    const int *constptr = new int(85);
    ptr = new int; // not allowed
    *ptr=5445; // not allowed < br/>


  1. Circle copy(const Circle &);
    这里 const Circle 意味着 Circle 的值只是可读的,如果我们试图改变函数中 Circle 的值,那么它会产生误差。
  2. friend Circle copy(Circle&) const;
    这种类型的函数不是用于非成员变量,而是用于类或结构。 这里整个函数都是用 const 关键字限定的,这意味着我们不能改变 对象成员变量。 例如 < br/>
    class A{ public :
int  var;
void fun1()
{ var = 50; // allowed
}
void fun2()const
{ var=50; //not allowed
}
};