草庐IT

const-reference

全部标签

c++ - 允许成员为 const,同时仍然支持类上的 operator=

我的类中有几个成员是const,因此只能通过初始化列表进行初始化,如下所示:classMyItemT{public:MyItemT(constMyPacketT&aMyPacket,constMyInfoT&aMyInfo):mMyPacket(aMyPacket),mMyInfo(aMyInfo){}private:constMyPacketTmMyPacket;constMyInfoTmMyInfo;};我的类可以用在我们内部定义的一些容器类(例如vector)中,这些容器需要在类中定义operator=。当然,我的operator=需要做这样的事情:MyItemT&MyItemT

C++ - 对`vtable 的 undefined reference

我想知道为什么会出现此错误:对“BaseRenderer的vtable”的undefinedreference我尝试四处搜索,但似乎无法弄清楚。我试过摆脱虚函数,删除构造函数等。我的BaseRenderer.hclassBaseRenderer:publicRenderer{Q_OBJECTpublic:BaseRenderer();BaseRenderer(QWidget*parent);voidpaintGL();virtual~BaseRenderer();publicslots:voidloadDialog();signals:protected:MeshloadMesh(st

c++ - 警告 C4172:返回对绑定(bind)到局部变量的 const std::string 的引用。它有多安全?

我刚刚在工作中构建我们的一个项目,我看到添加了一个新功能:conststd::string&ClassName::MethodName()const{return"";}编译器给出警告:WarningC4172:returningaddressoflocalvariableortemporary我认为编译器是对的。这个函数的安全性如何?请注意,该函数不会返回constchar*,这没有问题,因为字符串文字具有静态存储持续时间。它返回对conststd::string的引用 最佳答案 是的,它不安全。返回局部变量或临时变量的地址并取消

c++ - const char * 和 char *

我明白了char*s="HelloWorld!";存储在只读内存中,不能通过指针修改字符串文字。这和有什么不同constchar*s="HelloWorld!";另外'string'的类型是char*还是constchar*? 最佳答案 区别在于后者合法,前者不合法。这是在C++11中所做的更改。形式上,"HelloWorld!"的类型为constchar[13];它可以转换为constchar*。在过去,它的类型可能是char[13],可以转换为char*。C++通过添加const更改了数组的类型,但保留了对char*的转换,以便

c++ - 编译器如何在 C++ 中获取 const 的地址?

正如此处讨论的那样WhichmemoryareaisaconstobjectininC++?,编译器在编译代码时可能不会为常量分配任何存储,它们可能会直接嵌入到机器代码中。那么编译器如何获取常量的地址呢?C++代码:voidf(){constinta=99;constint*p=&a;printf("constant'svalue:%d\n",*p);} 最佳答案 一个常量是否会被分配任何存储空间完全取决于编译器。允许编译器根据As-If执行优化规则,只要程序的可观察行为没有改变,编译器可能会为a分配存储空间,也可能不会。请注意,标

C++ 通过 const 引用传递 const 指针

嗯,在VC2012中很奇怪,我似乎无法弄清楚通过const引用将const指针传递到模板参数是非const指针的模板类函数的语法,即:templatestructFoo{voidAdd(constT&Bar){printf(Bar);}};voidmain(){Foofoo;constchar*name="FooBar";foo.Add(name);//Causeserror}所以我在这里简化了我的问题,但基本上我希望“Add”的参数有一个constTieconstchar*。我试过:voidAdd(const(constT)&Bar);typedefconstTConstT;void

c++ - 对 `typeinfo for class' 的 undefined reference 和对 `vtable for class' 的 undefined reference

这个问题在这里已经有了答案:Undefinedsymbols"vtablefor..."and"typeinfofor..."?(5个答案)关闭9年前。我正在处理C++中的继承。我想写一个程序来对两个数组进行加法和减法。这是我的代码:#include#include#includeusingnamespacestd;classroot{protected:intsize;double*array;public:virtual~root(){}virtualroot*add(constroot&)=0;virtualroot*sub(constroot&)=0;virtualistrea

c++ - const 函数重载

这个问题在这里已经有了答案:FunctionswithconstargumentsandOverloading(3个答案)关闭9年前。我很困惑为什么下面的代码没有产生任何错误,因为传递给显示的参数是相同的类型,即char。const真的有区别吗?#includeusingnamespacestd;voiddisplay(char*p){cout编辑根据答案,永远不会调用第一个显示,这是正确的,输出也是如此。但假设我这样做:intmain(){char*p="Hello";display(p);//nowfirstdisplayiscalled.display("World");}编译器

c++ - "most important const"与 auto_ptr : Why the code does not compile?

以下代码无法在VisualC++2008或2010上编译:#includestructA{};std::auto_ptrfoo(){returnstd::auto_ptr(newA);}conststd::auto_ptrbar(){returnstd::auto_ptr(newA);}intmain(){conststd::auto_ptr&a=foo();//mostimportantconstconststd::auto_ptr&b=bar();//errorC2558://class'std::auto_ptr'://nocopyconstructoravailableorco

c++ - 对 std::ios_base::Init::Init() 的 undefined reference

我正在使用C++学习OOP,但遇到了问题。我确定这是一个内存分配问题,但似乎无法理解它。任何帮助将不胜感激。我的客户端代码#include#include"Box.cpp"usingnamespacestd;intmain(){Box*box=newBox;return0;}我的盒子类...#includeusingnamespacestd;classBox{private:doublewidth;doubleheight;doubleperimeter;doublearea;public:Box(){coutwidth;}doublegetHeight(){//returnthis-