草庐IT

Base_Struct

全部标签

c++ - 这是未定义的行为还是 struct init 的错误?

请考虑这段代码:#includeintmain(){structA{intx;inty;intz;intfoo(){std::coutxyzxyzx=1;this->z=10;return2;}};Ab{b.foo(),b.z=b.moo(),3};std::cout我的VS2017(x64版本)中的结果:enterfoo:0,0,0entermoo:5,0,0final:1,2,3ideone.com(gcc6.3)的结果https://ideone.com/OGqvjW):enterfoo:0,0,3entermoo:5,0,3final:1,2,2一个编译器立即将z成员设置为3,

c++ struct没有命名类型

我在头文件中定义一个结构,然后在相应的.cpp文件中设置其成员。为此,我使用了一个函数,该函数应该在其范围内创建一个(相同的)结构,然后返回它。像这样:在标题中:#includeclassGLWindow:publicQGLWidget,publicQGLFunctions{Q_OBJECTpublic:GLWindow(QWidget*parent=0);~GLWindow();//....structDrawable{GLuintvertexBuffer;GLuintindexBuffer;intfaceCount;QMatrix4x4transform;}cube;GLuintc

c++ - 隐式构造函数可用于从 Base 派生的所有类型,但当前类型除外?

以下代码总结了我的问题:templateclassBase{};templateclassDerived1:publicBase{};templateclassDerived2:publicBase{public://CopyconstructorDerived2(constDerived2&x);//AnEXPLICITconstructorthatdoesaspecialconversionforaDerived2//withothertemplateparameterstemplateexplicitDerived2(constDerived2&x);//Nowtheproble

c++ - struct 旁边的 < > 是做什么的?

好的,下面的代码是从另一个stackoverflow问题中复制过来的heretemplatestructremove_pointer{typedefTtype;};templatestructremove_pointer{typedeftypenameremove_pointer::typetype;};虽然我知道这是模板中的递归定义,但令我困惑的是这些行templatestructremove_pointer这是否意味着remove_pointer将导致T=int*?为什么不T=int**?感谢解释。 最佳答案 这是指针类型的特化

c++ - 如何将 Base 对象分配给 Derived 对象

我有一个关于C++的问题,如何将Base对象分配给Derived对象?或者如何将指向Base对象的指针分配给指向Derived对象的指针?在下面的代码中,两行是错误的。如何纠正?#includeusingnamespacestd;classA{public:inta;};classB:publicA{public:intb;};intmain(){Aa;Bb;b=a;//whathappend?coutb 最佳答案 将基对象分配给派生对象(或将基指针分配给派生指针)是没有意义的,因此C++将尽力阻止您这样做。异常(exception

c++ - 如何在 boost log 2.0 中设置 std::ios_base 标志,如 std::left?

我有一个广泛使用boostlog2.0的应用程序。现在我想为该应用程序设置一些默认标志,如std::setprecision(std::numeric_limits::digits10+1)、std::scientific和std::left。但是我该怎么做呢?一种方法是在我的主要功能的最开始创建一个记录器并创建一个虚拟日志消息。这将永久设置所需的标志。但是没有更好的方法来做到这一点吗?编辑回复:“OPshouldshowactualcode.”我有一个全局日志记录单例,称为L:classL{public:enumseverity_level{dddebug,ddebug,debug,

c++ - 在 C++ 中更有效地分配 struct 的内存

我正在尝试用C++构造一个结构,如下所示:structkmer_value{uint32_tcount:32;uint32_tpath_length:32;uint8_tacgt_prev:4;uint8_tacgt_next:4;}该结构目前占用12个字节的内存,但我想将大小减小到9个字节。有什么办法可以实现吗?谢谢。 最佳答案 没有可移植的解决方案。对于GCC,这将是struct__attribute__((packed))kmer_value{uint32_tcount:32;uint32_tpath_length:32;ui

c++ - 为什么 sizeof(Base) 与 sizeof(Derived) 没有区别

我觉得sizeof(Base)应该是12,为什么是16?没有虚函数,我得到4和8。classBase{public:inti;virtualvoidPrint(){cout预期结果:12,16实际结果:16,16 最佳答案 whysizeof(Base)isnotdifferentofsizeof(Derived)因为编译器引入了对齐。这是架构相关的,但为了简单起见,我假设我们指的是64位架构。Scenario64bit/Clang8.0.类型的对齐Base是8字节数:alignOfBase():#@alignOfBase()mov

c++ - 函数参数中的 struct 关键字和常量正确性

我的库中有一个不透明类型定义为:typedefstructMyOpaqueType*MyType;//easiertotypeforclientcode我不能使用typedef传递指向const结构的指针,所以一些函数看起来像:voidUsePointerToConst(conststructMyOpaqueType*)代替:voidUserPointerToConst(constMyType)//can'tuse,isreallyconstantpointer所以,鉴于此,我有两个问题:参数列表中的struct关键字是否只在C中是必需的?有一个更好的方法吗?我应该创建一个typede

c++ - 如何从 ctime 巧妙地初始化 struct tm

考虑这两种从格式化为字符串的日期获取纪元时间的方法:#includeintmain(){structtmtm_init={0};strptime("2012-10-2616:00","%Y-%m-%dT%H:%M",&tm_init);longepoch=mktime(&tm_init);structtmtm_rand;strptime("2012-10-2616:00","%Y-%m-%dT%H:%M",&tm_rand);epoch=mktime(&tm_rand);return0;}来源:http://ideone.com/3xMUm8.本质上的区别在于tm_init是用0初始化