草庐IT

has_begin_member

全部标签

c++ - RAII : Initializing data member in const method

在RAII中,资源在被访问之前不会被初始化。但是,许多访问方法都声明为常量。我需要调用一个mutable(非常量)函数来初始化一个数据成员。示例:从数据库加载structMyClass{intget_value(void)const;private:voidload_from_database(void);//Loadsthedatamemberfromdatabase.intm_value;};intMyClass::get_value(void)const{staticboolvalue_initialized(false);if(!value_initialized){//The

c++ - 模板类 : static members not inherited

这个问题在这里已经有了答案:Whydoesn'taderivedtemplateclasshaveaccesstoabasetemplateclass'identifiers?(4个答案)关闭7年前。下面的代码templatestructBase{staticconstinta=c+5;};templatestructDerived:Base{staticconstintb=a+5;};...编译失败因为awasnotdeclaredinthisscope.明确指定Base::a有效,但从逻辑上讲这不是必需的,因为我们是从Base派生的.这是预期的行为(以及为什么)还是我遗漏了什么?

C++ 多态性 : Is there any way to find the address of an object's member function?

如果我有一个纯虚拟基类及其多个派生...classBase{public:virtualvoidmethod1()=0;}classDerived1:publicBase{public:voidmethod1()override{...}}classDerived2:publicBase{public:voidmethod1()override{...}}有什么方法可以让持有未知派生类型对象的Base*的代码确定它持有的对象的method1()函数的地址Base*指针指向?我想做的是这样的:voidsomeOtherFunction(Base*pb){printf("IfIcallpb

c++ - Ptr-to-member 模板参数的 Friend 语法

好吧,所以我认为这是一个纯粹的C++语法问题。我有一个用ptr-to-member作为其模板参数之一定义的类:templateclassMy_list{我有另一个简单的类,它是这个类的友元。通常,我会写:classMy_friend_class{templatefriendclassMy_list;};...但是,因为第二个模板参数不是那么自由...我该如何表达友元?谢谢! 最佳答案 也许更好的问题是“为什么?”而不是“如何?”您到底想达到什么目的?当您的C++代码变得复杂和丑陋时,这通常是一个警告,表明您正在错误地处理问题。

c++ - boost Asio : waiting until thread_group has processed all posted tasks?

我有一个对象,一旦创建就会在后台执行许多任务,但应该阻塞直到/所有/发布的任务完成。即:structrun_many{boost::asio::io_servicem_io_service;boost::thread_groupm_threads;boost::asio::signal_setm_signals;voidevaluate(std::stringwork,inti){/*...*/}voidrun_tasks(inttasks,std::stringwork){{boost::asio::io_service::workw(m_io_service);//for(inti

c++ - 使用前向声明时如何修复 "field has incomplete type"错误

如注释中所述,此代码抛出编译器错误error:field‘fTarget’hasincompletetype。为什么会这样?我只是分配那个字段而不做任何需要知道里面是什么的操作......或者我是?也许它无法弄清楚复制构造函数?classFSRVertex;//fwdclassFSREdge{public:charfC;FSRVertexfTarget;//compilererrorFSREdge(charc,FSRVertextarget):fC(c),fTarget(target){}//compilererror};classFSRVertex{public:boost::uno

c++ - vector动态内存分配之private member vector

我是C++新手(我学过Fortran编程),我想为多维表动态分配内存。该表是私有(private)成员变量:classtheclass{public:voidsetdim(void);private:std::vector>thetable;}我想用函数setdim()设置表格的尺寸。voidtheclass::setdim(void){this->thetable.assign(1000,std::vector(2000));}我编译这个程序没有问题,但是当我执行它时,我遇到了一个段错误。对我来说奇怪的是,这段代码(见下文)完全符合我的要求,只是它没有使用我类的私有(private)

C++ 64 位 - 无法读取符号 : Archive has no index; run ranlib to add one

我正在尝试使用静态库在LinuxRHAS5.364位上生成一个非常简单的二进制文件。test1.cpp,生成的.o将被嵌入到静态库中。voidctest1(int*i){*i=5;}和prog.cpp#includevoidctest1(int*);intmain(){intx;ctest1(&x);printf("Valx=%d\n",x);return0;}如果我用32位编译,没问题:--(0931:Wed,06Apr11:$)--g++-m32-Wall-cctest1.cpp--(0931:Wed,06Apr11:$)--filectest1.octest1.o:ELF32-b

c++ - VC++ 警告 C4356 : static data member cannot be initialized via derived class

以下代码发出此警告,但它似乎工作正常,因为A::st和B::st都已初始化并且实际上代表相同的字符串。据我了解,这是格式错误的代码,不应编译(我检查了clang)。我想知道为什么VC++不发出错误而是发出警告?#include#includeclassA{public:staticconststd::stringst;};classB:publicA{};conststd::stringB::st="abcd";//warningC4356:'A::st':staticdatamembercannotbeinitializedviaderivedclassintmain(){std::

c++ - Boost 1.48.0 upgrade_to_unique_lock on Linux : Has something changed since 1. 47 还是我做错了什么?

我有一个小cppsource和hsource一些类的文件。它使用sharedmutexesandsharedlocks.它使用boost1.48.0在Windows上编译时没有错误。它还在linux上编译(之前使用boost1.47)。但是现在有这样的代码:boost::shared_mutexmut_;//...boost::upgrade_locklock(mut_);boost::upgrade_to_unique_lockuniqueLock(lock);导致奇怪的错误:====Buildingcf-fs(debug)====Creatingbin/obj/Debug/cf-f