草庐IT

External_variable

全部标签

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++ - Clrdump (C++) 错误 LNK2019 : unresolved external symbol __imp__RegisterFilter@8 referenced in function _main

我正在使用带有pvcs编译器的makefile系统(使用MicrosoftVisualC++,2008编译器),我收到了几个以下形式的链接错误:errorLNK2019:unresolvedexternalsymbol__imp__RegisterFilter@8referencedinfunction_main尽管使用了extern"C"声明,但还是会发生这种情况,即:extern"C"intCLRDUMP_APIRegisterFilter(LPCWSTRpDumpFileName,unsignedlongDumpType);此外,在makeexe.mak中,库链接如下:$(编译库

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++ - std::condition_variables 可以用作计数信号量吗?

这是CanC++11condition_variablesbeusedtosynchronizeprocesses?的后续行动.std::condition_variable对象可以用作计数信号量吗?我认为不是因为对象似乎绑定(bind)到std::mutex,这意味着它只能用作二进制信号量。我在网上看过,包括here,here,和here,但找不到将这些对象用作计数信号量的引用或示例。 最佳答案 是的。structcounting_sem{counting_sem(std::ptrdiff_tinit=0):count(init)

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++ - 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调用的一部