草庐IT

c++ - 在 C++ 中将非常量引用传递给右值

在以下代码行中:bootrec_reset(File(path,size,off),blksize);使用原型(prototype)调用函数:staticvoidbootrec_reset(File&file,ssize_tblksize);我收到此错误:libcpfs/mkfs.cc:99:53:error:invalidinitializationofnon-constreferenceoftype'File&'fromanrvalueoftype'File'libcpfs/mkfs.cc:30:13:error:inpassingargument1of'voidbootrec_r

c++ - 在 C++ 中将非常量引用传递给右值

在以下代码行中:bootrec_reset(File(path,size,off),blksize);使用原型(prototype)调用函数:staticvoidbootrec_reset(File&file,ssize_tblksize);我收到此错误:libcpfs/mkfs.cc:99:53:error:invalidinitializationofnon-constreferenceoftype'File&'fromanrvalueoftype'File'libcpfs/mkfs.cc:30:13:error:inpassingargument1of'voidbootrec_r

c++ - 为什么人们要编写返回非常量引用的私有(private)字段 getter?

我们都同意公共(public)变量不利于封装等等。但是,我注意到很多代码都在做这种事情:classfoo{private:intinteger_;stringsomeString_;//othervariablespublic:int&integer(){returninteger_;}string&someString(){returnsomeString_;}//other"functions"}intmain(){foof;f.integer()=10;f.someString()="something";return0;}我已经看到很多地方都在使用它,但我不明白为什么。基本上,

c++ - 为什么人们要编写返回非常量引用的私有(private)字段 getter?

我们都同意公共(public)变量不利于封装等等。但是,我注意到很多代码都在做这种事情:classfoo{private:intinteger_;stringsomeString_;//othervariablespublic:int&integer(){returninteger_;}string&someString(){returnsomeString_;}//other"functions"}intmain(){foof;f.integer()=10;f.someString()="something";return0;}我已经看到很多地方都在使用它,但我不明白为什么。基本上,

c++ - 通过非常量指针修改 const

我对以下代码中发生的事情有点困惑:constinte=2;int*w=(int*)&e;//(1)casttoremoveconst-ness*w=5;//(2)cout在(1)中,w指向e的地址。在(2)中,该值更改为5。但是,当显示*w和e的值时,它们的值不同。但是如果你打印w指针和&e的值,它们的值/地址是相同的。为什么e仍然包含2,即使它已更改为5?它们是否存储在单独的位置?还是临时的?但是w指向的值怎么还是e的地址呢? 最佳答案 正如我在评论中所说,一旦你修改了const值,你就处于未定义的行为状态,所以谈论正在发生的事情

c++ - 通过非常量指针修改 const

我对以下代码中发生的事情有点困惑:constinte=2;int*w=(int*)&e;//(1)casttoremoveconst-ness*w=5;//(2)cout在(1)中,w指向e的地址。在(2)中,该值更改为5。但是,当显示*w和e的值时,它们的值不同。但是如果你打印w指针和&e的值,它们的值/地址是相同的。为什么e仍然包含2,即使它已更改为5?它们是否存储在单独的位置?还是临时的?但是w指向的值怎么还是e的地址呢? 最佳答案 正如我在评论中所说,一旦你修改了const值,你就处于未定义的行为状态,所以谈论正在发生的事情

c++ - 虚函数 const 与虚函数非常量

classBase{public:virtualvoidfunc()const{coutfunc();deleted;return0;}为什么输出会打印“Thisisconstantbase”。但是,如果我在func()的基本版本中删除const,它会打印“Thisisnonconstantderived”d->func()应该正确调用Derived版本,即使Basefunc()是const对吗? 最佳答案 virtualvoidfunc()const//inBasevirtualvoidfunc()//inDerivedconst

c++ - 虚函数 const 与虚函数非常量

classBase{public:virtualvoidfunc()const{coutfunc();deleted;return0;}为什么输出会打印“Thisisconstantbase”。但是,如果我在func()的基本版本中删除const,它会打印“Thisisnonconstantderived”d->func()应该正确调用Derived版本,即使Basefunc()是const对吗? 最佳答案 virtualvoidfunc()const//inBasevirtualvoidfunc()//inDerivedconst

c++ - 为什么用非常量声明静态变量不会在g++中给出错误

intmain(){inta=1;staticintb=a;}这段代码在gcc4.3.2中给出了错误error:initializerelementisnotconstant.但它在g++4.3.2上运行良好。有人请解释为什么这在g++4.3.2中没有给出错误。 最佳答案 因为它在C++中有效(在C中无效)。(C++11,6.7p4)"Constantinitialization(3.6.2)ofablock-scopeentitywithstaticstorageduration,ifapplicable,isperformedb

c++ - 为什么用非常量声明静态变量不会在g++中给出错误

intmain(){inta=1;staticintb=a;}这段代码在gcc4.3.2中给出了错误error:initializerelementisnotconstant.但它在g++4.3.2上运行良好。有人请解释为什么这在g++4.3.2中没有给出错误。 最佳答案 因为它在C++中有效(在C中无效)。(C++11,6.7p4)"Constantinitialization(3.6.2)ofablock-scopeentitywithstaticstorageduration,ifapplicable,isperformedb