问道游戏好玩吗
问道游戏好玩吗?
更新时间:2022-10-23 09:10
最满意答案
const T& data() const { return data_; } ^^^^^
意味着它将返回一个
const
引用给T
(这里是data_
)Class c; T& t = c.data() // Not allowed. const T& tc = c.data() // OK.
const T& data() const { return data_; } ^^^^^
意味着该函数不会修改该类的任何成员变量(除非该成员是
mutable
)。void Class::data() const { this->data_ = ...; // is not allowed here since data() is const (unless 'data_' is mutable) this->anything = ... // Not allowed unless the thing is 'mutable' }
const T& data() const { return data_; } ^^^^^
means it will return a
const
reference toT
(heredata_
)Class c; T& t = c.data() // Not allowed. const T& tc = c.data() // OK.
const T& data() const { return data_; } ^^^^^
means the function will not modify any member variables of the class (unless the member is
mutable
).void Class::data() const { this->data_ = ...; // is not allowed here since data() is const (unless 'data_' is mutable) this->anything = ... // Not allowed unless the thing is 'mutable' }
相关问答
更多-
为什么在方法或函数名称之前和之后使用const关键字?(Why using the const keyword before and after method or function name?)[2023-07-22]
const T& data() const { return data_; } ^^^^^ 意味着它将返回一个const引用给T (这里是data_ ) Class c; T& t = c.data() // Not allowed. const T& tc = c.data() // OK. const T& data() const { return data_; } ^^^^^ 意味着该函数不会修改该类的任何成员变量(除非该成员是 ... -
const仅适用于变量,不适用于对象(或函数)属性。 如上所述,您可以使用Object.defineProperty来定义无法更改的对象属性: function Constants() { } Object.defineProperty(Constants, 'i', { value: '1', writable: false, // this prevents the property from being changed enumerable: true, // this w ...
-
txt是char[100]类型。 它必须转换为char *才能传递给fun ; 此转换产生右值 。 你不能从右值创建一个非const引用。 为了说明,考虑如果fun定义如下,会发生什么情况: void fun(char *&x) { x++; } 下面的代码会做什么(假设它可以编译)? char txt[100]="kolokwium"; fun(txt); // Huh? txt is of type char[100]. It has to be conve ...
-
实际上, operator T() const是一个const-member函数,在this函数中, this指针指的是一个const对象,它反过来又生成了getterCount const 。 解决方案是将getterCount声明为可变,如下所示: mutable size_t getterCount; 现在getterCount可以在const成员函数中递增,这也意味着,即使对象是const ,它也可以递增: void f(OneCell const & cell) { //incremen ...
-
const关键字的用法(Usage of the const keyword)[2022-03-14]
编译器(在这种情况下是GCC)是否足够聪明,注意到变量在函数期间永远不会发生变化,并且编译它就好像我会明确地添加const一样? 不必要。 例如: void some_function(int *ptr); // defined in another translation unit int foo(int a) { some_function(&a); return a + 1; } 编译器无法看到some_function作用,因此它不能假设它不会修改a 。 链接时优化也许可以看到s ... -
这是为什么要避免using std一个很好的例子。 为了调试这个问题,删除using std ,并添加std::在你想要标准库的行为的地方。 幸运的是,只有一个这样的地方,即模板类中的swap功能: void swap(vector
&v) { std::cout << "swap()" << std::endl; } 现在再次尝试编译以查看防止使用const与const swap 的错误 : prog.cpp:19:5:error:传递const vector 因为this参数 ... -
const关键字是什么必要的(what is const keyword necessary)[2023-11-11]
在C ++临时表中不能绑定到非常量引用。 在 int &p = foobar(); 右值表达式foobar()生成一个不能绑定到p的临时值,因为它是一个非const引用。 const int &x = foobar(); 将临时的x附加到对const的引用延长其生命周期。 这是完全合法的。 In C++ temporaries cannot be bound to non-constant references. In int &p = foobar(); The rvalue expres ... -
在这个位置的const声明返回类型为const 。 此位置的const只能用于成员函数,并且意味着该函数不能/不会修改任何成员不可mutable变量(对象常量)。 这是上述2个组合。 const in this position declares the return type as const. const in this position is only usable for member functions and means the function cannot / won't modify an ...
-
函数参数中的struct关键字和const-correctness(struct keyword in function parameter, and const-correctness)[2022-01-31]
参数列表中的struct关键字仅在C中是必需的吗? 是。 请参阅Jens Gustedt的回答。 有一个更好的方法吗? 只需typedef结构,而不是指针。 这更好,因为 你只需要一个typedef而不是一个{ MyOpaqueType , MyOpaqueType * , MyOpaqueType const * , MyOpaqueType *const和MyOpaqueType const *const }以及所有涉及restrict变体(在C ++中不存在), 用户很清楚指针语义是否适用,即传递数据 ... -
const int* sample1(); int const* sample3(); 这些功能完全相同。 它们返回指向常量内存的指针(此内存不能通过此指针更改)。 但我们可以改变指针本身。 例如增加它。 int* const sample2(); 此函数返回指向非常量内存的常量指针。 我们不能改变指针本身,但我们可以改变它所指向的内存。 const int* sample1(); int const* sample3(); These functions are identical. They re ...