草庐IT

Variable_init

全部标签

C++ : Initializing base class constant static variable with different value in derived class?

我有一个带有常量静态变量a的基类A。我需要类B的实例对静态变量a具有不同的值。这怎么能实现,最好是静态初始化?classA{public:staticconstinta;};constintA::a=1;classB:publicA{//???//Howtoset*a*toavaluespecifictoinstancesofclassB?}; 最佳答案 你不能。所有派生类共享一个静态变量实例。 关于C++:Initializingbaseclassconstantstaticvaria

c++ - 错误 : Variable length array of Non-POD element type 'string'

在开始之前,我必须首先声明,我已经研究过针对此错误的可能解决方案。不幸的是,它们都与不使用数组有关,这是我项目的要求。另外,我目前正在学习CS入门类(class),所以我的经验几乎没有。数组的用途是从文件中收集名称。因此,为了初始化数组,我计算了名称的数量并将其用作大小。问题是标题中所述的错误,但我仍然使用一维数组时看不到解决方法。主要.cpp#include#include#include#include#include#include"HomeworkGradeAnalysis.h"usingnamespacestd;intmain(){ifstreaminfile;ofstrea

c++ - std::condition_variable::wait_until 相对于 std::this_thread::sleep_for 有什么优势吗?

在时间等待场景中:oursoftwareworksinthebackground,andsynchronizesdatawiththeserverinevery20-30minutes.我想用std::this_thread::sleep_for但我的上级强烈反对任何形式的sleep功能。他推荐std::condition_variable::wait_until(lock,timeout-time,pred)不知道在这种情况下sleep_for有什么缺点吗? 最佳答案 正如评论中已经指出的那样,这仅取决于您的用例。两者之间的主要区

c++ - std::condition_variable::wait with predicate

在std::condition_variable的文档中,有一个以谓词函数作为参数的wait()重载。该函数将等到谓词函数为真的第一个wake_up。在documentation据说这等同于:while(!pred()){wait(lock);}还有:Thisoverloadmaybeusedtoignorespuriousawakeningswhilewaitingforaspecificconditiontobecometrue.Notethatbeforeentertothismethodlockmustbeacquired,afterwait(lock)exitsitisals

c++ - 为什么没有为所有变量报告 "unused variable"警告?

这个问题在这里已经有了答案:g++doesnotshowa'unused'warning(3个答案)关闭8年前。我有这个代码://initializerlists#include#includeintmain(){intvalues[]{1,2,3};std::vectorv{4,5,6};std::vectorcities{"London","NewYork","Paris","Tokio"};return0;}然而,gcc编译器只针对values数组给我unusedvariable警告。为什么v和cities没有被报告?

c++ - 扭曲的逻辑 : a global variable in one file refers to an extern variable but is also referred by that extern variable

文件A.cpp:#includeexternintiA;externintiB=iA;intmain(){std::cout文件B.cppexternintiB;externintiA=2*iB;编译链接运行,out进来debug和release模式是0,0我的问题是它是如何工作的,为什么在链接阶段没有问题?我正在使用VC++2003。 最佳答案 初始化程序覆盖了extern关键字,因此这没有什么“神奇”:您只是在不同的翻译单元中声明和定义两个完全不相关的变量。来自StandardforProgrammingLanguageC++-

c++ - G++ 4.6 -std=gnu++0x : Static Local Variable Constructor Call Timing and Thread Safety

voida(){...}voidb(){...}structX{X(){b();}};voidf(){a();staticXx;...}假设在进入main之后,f被多个线程(可能竞争)多次调用。(当然,唯一对a和b的调用是上面看到的那些)以上代码在-std=gnu++0x模式下用gccg++4.6编译时:Q1。是否保证至少调用一次a()并在调用b()之前返回?也就是说,在第一次调用f()时,x的构造函数是否会同时调用一个自动持续时间局部变量(非静态)(而不是在全局静态初始化时间)?Q2。是否保证b()只会被调用一次?即使两个线程第一次同时在不同的核上执行f?如果是,GCC生成的代码通过

c++ - Microsoft 的 GCC 选项 init_priority 是什么?

在编译和链接C++库或程序时,无法保证翻译单元之间静态C++对象的初始化顺序。GCCoffersinit_priority解决静态存档、共享对象和程序的问题:init_priority(priority)    InStandardC++,objectsdefinedatnamespacescopeareguaranteedto    beinitializedinanorderinstrictaccordancewiththatoftheir    definitionsinagiventranslationunit.Noguaranteeismadefor    initializ

c++ - GMock : How to return mock class variable as the return value

我是第一次尝试使用GMock(用于C++的谷歌模拟框架)。我有以下类(class):classLocalCache{public:virtualtime_tGetCurrentTime()=0;virtualintAddEntry(conststd::stringkey,std::string&value);virtualintGetEntry(conststd::stringkey,std::string&value);};GetEntry方法调用GetCurrentTime调用。我想模拟GetCurrentTime方法,以便我可以在测试中提前时钟以测试作为GetEntry调用的一部

c++ - 与 std::condition_variable 相比,使用 std::atomic 的方法 wrt 在 C++ 中暂停和恢复 std::thread

这是一个单独的问题,但与我之前提出的问题有关here我正在使用std::thread在我的C++不断轮询某些数据并将其添加到缓冲区的代码。我用C++lambda像这样启动线程:StartMyThread(){thread_running=true;the_thread=std::thread{[this]{while(thread_running){GetData();}}};}thread_running是一个atomic在类头中声明。这是我的GetData功能:GetData(){//Someheavylogic}接下来我还有一个StopMyThread我设置的功能thread_r