草庐IT

c++ - 如何使 exuberant-ctags 解析 "using"样式的 typedef?

如何让ctags解析using类型别名,如下所示?usingFooPtr=std::shared_ptr;它只选取那些用typedef声明的。 最佳答案 从Exuberant-ctags派生的Universal-ctags(https://ctags.io)在C++源文件中处理“使用”:[jet@localhost]/tmp%cat/tmp/foo.cppusingFooPtr=std::shared_ptr;[jet@localhost]/tmp%u-ctags-o-/tmp/foo.cppFooPtr/tmp/foo.cpp/^

c++ - 继承:将 derived& 作为 arg 传递给期望 base& 的函数

考虑这个示例代码:#includeclassbase{public:base(){std::cout为什么调用base::base(constbase&)没问题,但是调用derived::derived(constbase&)不是?两者都需要一个基本引用,并且都被赋予一个派生引用。我的理解是派生"is"基础。为什么编译器坚持使用derived::derived(constderived&)而它在提供时使用base::base(constbase&)没有问题引用派生类型的对象? 最佳答案 显然,“删除”其中一项默认内容并没有实际完全删

C++ 库 "internal use only"编码风格

我正在用C++编写一个库。所有类和全局函数都在mylibrary命名空间内声明。我需要创建一些仅供内部使用的类:使用这个库的人应该立即注意到哪些类不打算在库外使用。不幸的是,我不能使用私有(private)函数,因为这会弄乱所有封装。我不能使用私有(private)类,因为内部类必须由同一个库中的“普通”类访问。我想这样做的一个好方法是创建命名空间mylibrary::internal并将所有“私有(private)”内容放入其中。这是正确的方法吗?还有其他常见的方法吗? 最佳答案 为您的内部功能使用匿名命名空间。这将确保没有外部代

c++ - 使用 'using' 关键字获取指向类方法的指针

我使用的是VisualStudio2010。为什么我无法获得指向子类中“升级”为public的类方法的指针?以下代码无法编译:#include#includeclassParent{protected:voidfoo(){std::coutf=std::bind(&Child::foo,&c);f();return0;}它给出了错误:errorC2248:'Parent::foo':cannotaccessprotectedmemberdeclaredinclass'Parent' 最佳答案 编译here.我想你只是忘了在你的编译器

c++ - g++ : Using singleton in an embedded application

我正在使用C++中的GNUARM工具链使用GCC4.8为CortexM3开发嵌入式应用程序。该应用程序使用了一些通过函数局部静态变量实例化的单例,就像这样(真实代码):GlobalDataTypeRegistry&GlobalDataTypeRegistry::instance(){staticGlobalDataTypeRegistryinst;returninst;}这是在C++中实现单例的经典方法。问题是一旦我使用这种实例化,输出代码大小就会激增,这显然意味着编译器/链接器添加了一些服务代码以正确初始化/销毁单例对象。这是允许重现问题的最小示例:这将编译成66k代码(-Os):s

c++ - 错误 : Use of class template requires template argument list

当我尝试运行我的程序时,此错误显示为“errorC2955:'FOURTEEN':useofclasstemplaterequirestemplateargumentlist”#includeusingnamespacestd;templateclassFOURTEEN{private:Ta[n];public:voidReadData();voidDisplayData();};voidFOURTEEN::ReadData(){for(inti=0;i>a.[i];}voidFOURTEEN::DisplayData(){for(inti=0;i>a.[i]P;//Readdatai

c++ - 谷歌模拟 : Is it ok to use global mock objects?

在所有关于gmock的文档中,我总是发现要在测试中实例化模拟对象,就像这样:TEST(Bim,Bam){MyMockClassmyMockObj;EXPECT_CALL(MyMockObj,foo(_));...}因此,对象在每次测试时都会被创建和销毁。我相信为每个测试夹具创建和销毁对象也完全没问题。但我想知道是否也可以拥有模拟对象的文件全局实例,就像这样:MyMockClassmyMockObj;TEST(Bim,Bam){EXPECT_CALL(MyMockObj,foo(_))...}我试过了,到目前为止我完全没有问题,一切似乎都很好。但也许我应该知道什么?只是因为我偶然发现了t

c++ - 地址 sanitizer : "SEGV on unknown adress" when using throw-catch and printf

当我扔东西时,例如一个int或一个charconst*,并使用printf检查错误,我得到一个AddressSanitizerError。我无法在网上找到任何类似的东西,而且我的代码示例非常基础,以至于我真的不明白它怎么可能会产生错误。精简代码示例:#includeintmain(){try{throw42;}catch(intmsg){printf("%d\n",msg);}return0;}运行代码时的控制台输出:ASAN:SIGSEGV===================================================================16533=

c++ - Boost Graph Library : How to use depth_first_visit, ColorMap 问题

初始问题:BoostGraphLibrary:PreventDFSfromvisitingunconnectednodes我正在尝试使用boost::depth_first_visit,但不知道如何提供ColorMap属性。我在这里尝试了示例中给出的方法:http://www.boost.org/doc/libs/1_58_0/libs/graph/example/loops_dfs.cpp我的(相关)代码:///Definevertexproperties.structNodeProperty{unsignedid;///Id.unsignedkind;///Kind.unsigne

c++ - 前向声明 : incomplete type 'enums::Category' used in nested name specifier 有问题

我想要一个围绕枚举的包装器,这将使我有机会将其转换为字符串,反之亦然。基类如下:templateclassStringConvertedEnum{public:staticstd::stringtoString(TEnume);staticTEnumtoEnum(std::string&str);protected:staticconststd::map_stringMapping;staticconststd::map_enumMapping;};然后我想要这样的东西:classCategory:publicStringConvertedEnum{public:enumEnum{Ca