草庐IT

copy_helper_block

全部标签

c++ - 由于缺乏反射,在 C++ 中不可避免地会有一个大的 switch block 吗?

这个问题在这里已经有了答案:Isthereawaytoinstantiateobjectsfromastringholdingtheirclassname?(12个答案)关闭9年前。假设我有一个类层次结构:classShape{};classCircle:publicShape{}classSquare:publicShape{}...hundredsofothershapescontinueon...当给定形状类的名称作为字符串时,我需要实例化该类的对象。在java中,我可以做这样的事情(伪代码!)ShapecreateShape(Stringname){returnnewClass

c++ - 如何确定 catch (...) block 中的当前异常?

这个问题在这里已经有了答案:关闭12年前。PossibleDuplicate:Determiningexceptiontypeaftertheexceptioniscaught?跟进question,我想在catch(...)block中打印出当前异常——仅用于日志记录。那里的一个答案说没有标准的方法可以做到这一点,但我不喜欢拒绝:-)current_exception()是网络上多处提到的函数,但显然没有得到很好的支持。对此有什么想法吗?毕竟,即使是C也有errno。因为它可以被重新抛出(通过简单的**throw*),异常对象必须以某种方式可用。我正在使用MSVS9.0。编辑:结论似

c++ - 通过 copy-and-swap 分配与两个锁

借款HowardHinnant'sexample并将其修改为使用copy-and-swap,这op=线程安全吗?structA{A()=default;A(Aconst&x);//Assumeimplementscorrectlockingandcopying.A&operator=(Ax){std::lock_guardlock_data(_mut);usingstd::swap;swap(_data,x._data);return*this;}private:mutablestd::mutex_mut;std::vector_data;};我相信这是线程安全的(记住op=的参数是按

c++ - Code::Blocks 控制台应用程序不会显示输出

我在Code::Blocks中有一个应用程序,它是简单的Hello,World传统程序。#includeintmain(){std::cout程序构建并执行,但未显示输出。我检查了Code::Blocks中的项目属性,它肯定设置为控制台应用程序。关于这个问题有什么建议吗?编辑:输出仅在IDE中失败。单独运行时,生成的可执行文件功能完全符合预期。 最佳答案 可能您没有安装xterm。如果您使用的是Linux(Debian风格),您可以像这样使用包管理器安装它:sudoapt-getinstallxterm

c++ - Visual Studio C++ - 缺少 "Copy to Output Directory"

这个问题在这里已经有了答案:Automaticcopyfilestooutputduringapplicationbuilding(8个答案)关闭9年前。我在VisualStudio(2012)中创建了一个空的C++项目,当我在解决方案资源管理器中选择了某个文件时,在“属性”窗口中看不到“复制到输出目录”选项。为什么?

C++ std::system_error 与 common catch std::exception block 的用法

std::system_error处理带有相关错误代码的异常。是否可以使用公共(public)catchblock来获取std::system_error异常消息及其代码?像这样try{//codegeneratingexception}catch(conststd::exception&ex){//catchallstd::exceptionbasedexceptionslogger.log()唯一的方法是直接捕获std::system_error类型并在捕获基本异常类型之前获取其代码吗?广泛使用std::system_error的最佳方法是什么? 最佳答

c++ - 使用哪个 : move assignment operator vs copy assignment operator

我似乎不明白为什么要使用移动赋值运算符:CLASSA&operator=(CLASSA&&other);//moveassignmentoperator结束了,复制赋值运算符:CLASSA&operator=(CLASSAother);//copyassignmentoperator移动赋值运算符仅采用r值引用,例如CLASSAa1,a2,a3;a1=a2+a3;在复制赋值运算符中,other可以是使用复制构造函数或移动构造函数的构造函数(如果other是用右值初始化的,它可以是移动构造的——如果move-constructor定义了——)。如果它是copy-constructed,我

c++ - 通过 FFI block 从 Haskell 调用共享库,而从 C 程序链接时则不会

我正在尝试通过Haskell应用程序连接BaslerUSB3相机,但我遇到了一些困难。该相机带有一个C++库,使其相当简单。以下代码可用于获取相机源:extern"C"{voidbasler_init(){PylonAutoInitTermpylon;CInstantCameracamera(CTlFactory::GetInstance().CreateFirstDevice());camera.RegisterConfiguration((CConfigurationEventHandler*)NULL,RegistrationMode_ReplaceAll,Cleanup_Non

c++ - 如何避免在 catch block 中编写重复代码?

我正在为桌面应用程序项目使用QT4.8(C++),并编写如下异常处理:voidcallerMethod(){try{method1();}catch(Exception1&e){//displaycritcalerrormessage//abortapplication}catch(std::Exception&e){//printexceptionerrormessage}catch(...){//printunknownexceptionmessage}}voidmethod1(){try{//someinitializations//someoperations(hereexce

c++ - ‘operator=’ 的模糊重载与 c++11 std::move and copy and swap idiom

我收到以下错误:[matt~]g++-std=c++11main.cpp-DCOPY_AND_SWAP&&./a.outmain.cpp:Infunction‘intmain(int,constchar*const*)’:main.cpp:101:24:error:ambiguousoverloadfor‘operator=’in‘move=std::move((*©))’main.cpp:101:24:note:candidatesare:main.cpp:39:7:note:Test&Test::operator=(Test)main.cpp:52:7:note:Test&