草庐IT

c++ - sizeof(*this) 和结构继承

假设我有一个如下所示的结构:structParentStruct{virtualvoidXYZ(){getSize(sizeof(*this));}intmemberX;}还有另一个继承父结构的struct:structChildStruct:publicParentStruct{intmemberY;intmemberZ;}假设sizeof(int)==4,是否可以将12的值传递给函数getSize()从子结构调用(我目前得到的值是4)?我宁愿不必覆盖所有子结构中的XYZ(),因为我会有很多子结构。 最佳答案 正如其他人所说,th

c++ - 如何在结构内联中分配字符数组?

我正在尝试做这样的事情:structSomeStruct{constchar*bytes;constchar*desc;};SomeStructexample={{0x10,0x11,0x12,0x13},"10-13"};为什么这不起作用? 最佳答案 可能是因为{0x10,0x11,0x12,0x13}是一个char数组,而不是指向char的指针。试试SomeStructexample={"\x10\x11\x12\x13","10-13"};代替。 关于c++-如何在结构内联中分配字

c++ - C++标准库中有没有类似于没有成员的struct的类型?

有时需要将没有任何数据的虚拟值传递给某些模板。例如:templatestructBoundaryConditions{Xx;Yy;BoundaryConditions(typenameX::initxi,typenameY::inityi):x(xi),y(yi){...}};我们可能希望实现不带任何参数的自由边界条件。通过类型检查实现这样的事情非常容易:structNothing{};Nothingnothing=Nothing();structFree{typedefNothinginit;...};BoundaryConditionsfoo(nothing,100);所以我的问题

c++ - 如何使用 C++11 正确初始化 'struct stat'?

多年来,我一直像这样初始化我的structstat:#includestructstatfoo={0};具体来说,{0}将所有字段设置为零,相当于memset(&foo,NULL,sizeoffoo);。现在使用C++11,这已经开始产生警告:foo.cpp:2:19:warning:missingfield'st_mode'initializer[-Wmissing-field-initializers]structstats={0};^这是因为C++11的新初始化语法,警告暗示我没有初始化所有成员。在C++11中实例化和初始化structstat的首选方法是什么?

c++ - 正在阅读一个不是最近用 GCC 编写的未定义行为的成员吗?

C++reference有以下用于union的explanation,这个问题的有趣部分以粗体显示:Theunionisonlyasbigasnecessarytoholditslargestdatamember.Theotherdatamembersareallocatedinthesamebytesaspartofthatlargestmember.Thedetailsofthatallocationareimplementation-defined,andit'sundefinedbehaviortoreadfromthememberoftheunionthatwasn'tmos

c++ - 具有自定义结构的 <set> 包含重复项

我一直在学习C++。我被这个问题困住了。我有一个包含自定义结构的集合,该结构包含两个longint的a和b。我有一个自定义比较器结构,用于比较数字并在a或b不同时返回true。typedeflongintli;structnumber{number(lia1,lib1):a(a1),b(b1){}lia,b;};structcompare{booloperator()(constnumber&lhs,constnumber&rhs)const{returnlhs.a!=rhs.a||lhs.b!=rhs.b;}};intmain(){setnums;nums.insert(number

c++ - 如何在 std::map 中尝试放置 POD 结构?

我有一张int->{basictypes}的map,我需要存储。我想简单地创建一个struct{intf1,intf2;};并直接存储值,在存储过程中就地构造结构。我不希望有任何重复的键,所以try_emplace看起来很理想。我写了这段代码://mcve.cpp#include#includestructvarious{intf1,f2;};usingmap_t=std::map;voidexample(){map_tdict;//dict.try_emplace(1,2);dict.try_emplace(1,1,2);//dict.try_emplace(1,{1,2});}但是

c++ - 如何创建一个包含自身列表的结构?

我想创建一个结构,其中包含像这样的相同结构的列表:#includestructUrl{CStringstrUrl;std::listchildren;};intmain(){Urlu1,u2;u1.children.push_back(u2);}此代码未编译。但是当我将std::list替换为std::vector时,它工作正常。我怎样才能让它与std::list一起工作?输出窗口包含以下错误。c:\programfiles\microsoftvisualstudio\vc98\include\list(29):errorC2079:'_Value'usesundefinedstruc

c++ - Visual C++ 中的结构对齐

VisualC++提供编译器开关(/Zp)和packpragma来影响结构成员的对齐。但是,我似乎对它们的工作方式有一些误解。根据MSDN,对于给定的对齐值n,Thealignmentofamemberwillbeonaboundarythatiseitheramultipleofnoramultipleofthesizeofthemember,whicheverissmaller.让我们假设包值为8个字节(这是默认值)。在一个结构中,我认为任何大小小于8字节的成员的偏移量都是其自身大小的倍数。大小为8字节或更多的任何成员的偏移量都是8字节的倍数。现在执行以下程序:#include#p

c++ - 为什么可以声明同名的结构和非结构?

显然,ForreasonsthatreachintotheprehistoryofC,itispossibletodeclareastructandanon-structwiththesamenameinthesamescope.-(BjarneStroustrup-TheC++ProgrammingLanguage.4thEdition)例如:structAmbig{};//thestructmustbereferredtowiththeprefixstructvoidAmbig(structAmbig*buf){}我只是好奇最初的原因是什么?没有理解,这似乎是一个糟糕的语言设计的例