Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数名后不久的常量类型限定符 在C++中,有时我会看到如下的声明: return_type function_name( datatype parameter1, datatype parameter2 ) const { /*................*/}_C++_Constants_Member Functions - Fatal编程技术网

函数名后不久的常量类型限定符 在C++中,有时我会看到如下的声明: return_type function_name( datatype parameter1, datatype parameter2 ) const { /*................*/}

函数名后不久的常量类型限定符 在C++中,有时我会看到如下的声明: return_type function_name( datatype parameter1, datatype parameter2 ) const { /*................*/},c++,constants,member-functions,C++,Constants,Member Functions,这个常量类型限定符在这种情况下做什么?成员函数声明末尾的常量限定符表示可以对本身是常量的对象调用函数。const成员函数承诺不会更改任何不可变数据成员的状态 当然,const成员函数也可以在非const对象上调用,并且仍然做出相同的承诺 成员函数也可以在常量上重载。例如: class A { public: A(int val) : mValue(val) {} int value() const { return mValue; } void value(int

这个常量类型限定符在这种情况下做什么?

成员函数声明末尾的常量限定符表示可以对本身是常量的对象调用函数。const成员函数承诺不会更改任何不可变数据成员的状态

当然,const成员函数也可以在非const对象上调用,并且仍然做出相同的承诺

成员函数也可以在常量上重载。例如:

class A {
  public:
    A(int val) : mValue(val) {}

    int value() const { return mValue; }
    void value(int newVal) { mValue = newVal; }

  private:
    int mValue;
};

A obj1(1);
const A obj2(2);

obj1.value(3);  // okay
obj2.value(3);  // Forbidden--can't call non-const function on const object
obj1.value(obj2.value());  // Calls non-const on obj1 after calling const on obj2

这意味着它不修改对象,因此您可以使用const对象调用该方法

i、 e

意思是如果你有

const MyClass myClass;
你可以打电话

int cValue = myClass.ConvertToInteger();
没有编译错误,因为方法声明表明它不会更改对象的数据。

$9.3.1/3状态-

非静态成员函数可以声明为const、volatile或const volatile。这些限定符影响该指针的类型9.3.2。它们还影响成员函数的函数类型8.3.5;声明为const的成员函数是const成员函数,声明为volatile的成员函数是volatile成员函数,声明为const volatile的成员函数是const volatile成员函数

以下是总结:

常量限定符只能用于类非静态成员函数

b参与过载的功能的cv资格

struct X{
   int x;
   void f() const{
      cout << typeid(this).name();
      // this->x = 2;  // error
   }
   void f(){
      cout << typeid(this).name();
      this->x = 2;    // ok
   }
};

int main(){
   X x;
   x.f();         // Calls non const version as const qualification is required
                  // to match parameter to argument for the const version

   X const xc;
   xc.f();        // Calls const version as this is an exact match (identity 
                  // conversion)
}
struct X{
   int x;
   void f() const{
      cout << typeid(this).name();
      // this->x = 2;  // error
   }
   void f(){
      cout << typeid(this).name();
      this->x = 2;    // ok
   }
};

int main(){
   X x;
   x.f();         // Calls non const version as const qualification is required
                  // to match parameter to argument for the const version

   X const xc;
   xc.f();        // Calls const version as this is an exact match (identity 
                  // conversion)
}