简体   繁体   English

const auto 和 const type 之间有什么区别*

[英]what's difference between const auto and const type*

why error happend, I think const auto data_0 should be the same with const Data* data_1, what's the difference bwtween data_0 and data_1?为什么会发生错误,我认为 const auto data_0 应该与 const Data* data_1 相同,那么 data_0 和 data_1 之间有什么区别?

class Data {
public:
  Data(int val_) : val(val_) {
  }
  ~Data() {
  }
  void SetVal(int val_) {
    val = val_;
  }
private:
  int val;
};

Data* GetData(int val) {
  return new Data(val);
}

int main () {
  const auto data_0 = GetData(0);
  const Data* data_1 = GetData(0);
  data_0->SetVal(1);  // OK 
  data_1->SetVal(1);  // error: passing ‘const Data’ as ‘this’ argument discards qualifiers
  return 0;
}

const applies to the thing on its left, unless there is nothing then it applies to the thing on its right. const适用于它左边的事物,除非什么都没有,那么它适用于它右边的事物。

In const auto data_0 = GetData(0);const auto data_0 = GetData(0); , the const applies to whatever type auto deduces to, which in this case is Data* . const适用于auto推导出的任何类型,在这种情况下是Data* So, the final declaration is:所以,最终的声明是:

Data * const data_0 = GetData(0);

The const applies to the pointer itself, not the thing it is pointing at. const适用于指针本身,而不是它所指向的东西。 Thus, data_0 is a const pointer to a non-const Data object.因此, data_0是一个指向非常量Data对象的常量指针。 SetVal() is a non-const method, so it can be called on the object. SetVal()是一个非常量方法,因此可以在对象上调用它。

In const Data* data_1 = GetData(0);const Data* data_1 = GetData(0); , the const applies to Data . const适用于Data So, the final declaration is:所以,最终的声明是:

Data const * data_1 = GetData(0);

The const applies to the thing being pointed at, not to the pointer itself. const适用于所指向的事物,而不适用于指针本身。 Thus, data_1 is a non-const pointer to a const Data object.因此, data_1是一个指向 const Data对象的非常量指针。 SetVal() is not a const method, so it cannot be called on the object. SetVal()不是 const 方法,因此不能在对象上调用它。

what's difference between const auto and const type* const auto 和 const type 之间有什么区别*

const auto will be deduced from the initialiser and will be a const qualified type. const auto将从初始化器中推导出来,并且是一个 const 限定类型。 In case of data_0 , the type of the initialiser is a pointer to non-const Data and therefore the deduced type becomes a const qualified pointer to non-const Data ie Data* const .data_0情况下,初始化器的类型是指向非常量Data的指针,因此推导出的类型成为指向非常量Data的常量限定指针,即Data* const

const type* is a non-const qualified pointer to a const qualified object of type type . const type*是指向 type type的 const 限定对象的非常量限定指针。 The difference is that one is non-const pointer to const and the other is const pointer to non-const.区别在于一个是指向const的非常量指针,另一个是指向非常量的const指针。

why error happend为什么会发生错误

Because you call a non-const qualified member function through a pointer to const.因为你通过一个指向 const 的指针调用了一个非常量限定的成员函数。

I think const auto data_0 should be the same with const Data* data_1我认为 const auto data_0 应该与 const Data* data_1 相同

It isn't, and it shouldn't be.不是,也不应该是。

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

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