草庐IT

static成员

全部标签

c++ - 作为类成员的函数的定义

我有两个函数,它们是“Data”类的私有(private)成员:classDate{private:boolleapYear(inty);voidfillDate(intd,Monthm,inty);};那么,在哪里定义这个函数最好:在类定义中;在类外的头文件中;还是在“.cpp”文件中? 最佳答案 你可以在这里选择。以下是一些可以让您下定决心的想法:内联速度不再是问题,因为编译器现在擅长链接时优化。因此,性能不应成为此处的决定因素(编译速度也很重要,但这是另一袋蠕虫)。在类内部定义的小型内联成员函数可能是“记录”类所做的事情的简单

c++ - 没有匹配的成员函数来调用 child.value

当我尝试编译下面的代码时出现错误:src/main.cpp:51:48:error:nomatchingmemberfunctionforcallto'child_value'std::cout我不明白的是我能够在上面的循环中使用它。我只能假设它希望我使用kv.second.child_value(kv.second);代替。但是我希望它在for(auto&eb:mapb){.返回的xml上运行这段代码。#include"pugi/pugixml.hpp"#include#include#includeintmain(){conststd::maptagMap{{"descriptio

c++ - 使用 "operator T*()"而不是 "T* operator->()"进行成员访问

表达式x->y要求x是指向完整类类型的指针,或者当x是类的实例时,需要为x定义的operator->()。但是如果是后者,为什么不能我可以使用转换函数来代替(即将对象x转换为指针)?例如:structA{intmi;operatorA*(){returnthis;}};intmain(){Aa;a[1];//ok:equivalentto*(a.operatorA*()+1);a->mi;//ERROR}这给出了一条错误信息:错误:“->”的基操作数具有非指针类型“A”但问题是,为什么它不像a[1]那样使用a.operatorA*()呢? 最佳答案

c++ - 在成员初始化列表中使用 std::function

我有一个类型定义:typedefS32(iMyDataClass1::*getDataFunction_t)(void);和类型:structfunctionMap_t{std::vectorpDataFunctionTable;structdataRequestor_tdataRequestor;};然后我有一个成员变量:functionMap_tFnMap1;然后我在成员初始值设定项列表中设置:myClass::myClass():FnMap1({{&iMyDataClass1::getData1,&iMyDataClass1::getData2,&iMyDataClass1::g

c++ - 发布版本中成员函数和全局函数的性能差异

我实现了两个函数来执行两个Vector(不是std::vector)的叉积,一个是成员函数,另一个是全局函数,这里是关键代码(其他部分省略)//formemberfunctiontemplateSquareMatrixVector::outerProduct(constVector&vec3)const{SquareMatrixresult;for(unsignedinti=0;ivoidouterProduct(constVector&v1,constVector&v2,SquareMatrix&m){for(unsignedinti=0;i它们几乎相同,只是一个是有返回值的成员函数

c++ - 标准中的哪个地方说成员别名声明可以像静态成员一样使用?

考虑以下片段:#includestructA{inti;usingInt=int;};intmain(){std::cout在clang中编译执行正常和海湾合作委员会。我知道这看起来很明显,但我在标准(C++14)中找不到任何支持在main()中引用A::Int的内容。 最佳答案 这只是您的正常限定查找。来自[basic.lookup.qual]:Thenameofaclassornamespacememberorenumeratorcanbereferredtoafterthe::scoperesolutionoperator(5

c++ - 成员初始化器列表中的初始化

这个问题在这里已经有了答案:Constructorinitialization-listevaluationorder(3个答案)关闭7年前。classA{public:inta,b;A():a(1){b=3;}};如果我们创建这个类的一个对象:Aobj;那么a和b哪个先初始化?b=3赋值的过程中会不会有默认构造函数的参与?我指的是提供的答案:Ifyouuseassignmentthenthefieldswillbefirstinitializedwithdefaultconstructorsandthenreassigned(viaassignmentoperator)withact

c++ - 当使用静态成员将共享库静态链接到可执行文件时,我在 UNIX 和 WIN 上收到不同的结果。请解释为什么?

请考虑以下代码和平://1.Singleheaderfile.Imaginethatitissomestaticlibrary.//Counter.h#pragmaoncestructCounter{Counter(){++getCount();}staticint&getCount(){staticintcounter=0;returncounter;}};//2.Sharedlibrary(!)://main_DLL.cpp#include#include"counter.h"extern"C"{__declspec(dllexport)//forWINvoidmain_DLL()

c++ - 如何将 `std::vector` 成员变量 move 到方法的调用者?

请考虑以下代码classA{public:A(std::size_td):m_v(d)std::vectoroperator()(){returnm_v;}private:std::vectorm_v;};我想movem_v给operator()的来电者而不是复制它。我需要做什么?简单地写returnstd::move(m_v)并将返回类型更改为std::vector&&? 最佳答案 写returnstd::move(m_v)就够了。 关于c++-如何将`std::vector`成员变量

c++ - 返回从成员映射的迭代器获得的 const 引用是否安全?

如果_map是类型std::unordered_map的成员,返回对的引用是否安全_map.find(k)->second来自一个函数,或者这是未定义的行为(或者只是不好的做法)?它似乎按预期工作,但感觉有点像返回对临时对象的引用。我不确定这是否属实,或者是否会产生其他意想不到的后果。#includeclassContainer{public:usingKey=int;//orsomethingmoreinteresting//++++++++++++++++++++++++++++++++++++++++++//|Isitsafetoreturnareferencehere?|//+