首页 \ 问答 \ 问道游戏好玩吗

问道游戏好玩吗

问道游戏好玩吗?
更新时间: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 to T (here data_)

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 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 ...
  • 编译器(在这种情况下是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参数 ...
  • 在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关键字仅在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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。