jjzjj

explicit

全部标签

c++ - 显式构造函数和 static_cast

structFoo{explicitFoo(inta):m(a){}intpadd1,m,padd2;};voidBar(Foo){}intmain(){Bar(11);//OK,giveserrorautox=static_cast(37);x.m;}static_cast构造Foo对象是否可以,即使它的构造函数被标记为explicit?它适用于MSVC2013和GCChttp://ideone.com/dMS5kB 最佳答案 是的,static_cast将使用explicit构造函数。5.2.9Staticcast[expr.s

c++ - 警告 C4661 :no suitable definition provided for explicit template instantiation request

我写了一个类模板并在不同的DLL中使用它,所以希望隐藏部分实现。为此,我使用“模板实例化”,但导出它,像这样,这里是头文件:#include#includeusingnamespacestd;templateclass__declspec(dllexport)Templated{public:Templated();};template__declspec(dllexport)Templated;intmain(){cout并且定义在单独的文件(.cpp)中templateTemplated::Templated(){}templateTemplated;我的问题是我收到警告,即使实例

c++ - 用空花括号初始化

第一次尝试,一切正常:classBase{public:Base(){std::cout另一种实现方式(添加explicit):classBase{public:explicitBase(){std::cout我在cppreference上读到,在这两种情况下都将使用默认初始化并且没有区别。从列表初始化:Otherwise,Ifthebraced-init-listisemptyandTisaclasstypewithadefaultconstructor,value-initializationisperformed.从值初始化:ifTisaclasstypewithnodefaul

c++ - 错误 : no matching function for call to . .. 在返回语句

我在openSUSELeap15上的Qt5.9.4上使用GCC7。我有以下类(class):classManSuppProps:publicQObject{Q_OBJECTpublic:explicitManSuppProps(QStringparentName);explicitManSuppProps(){}explicitManSuppProps(constManSuppProps&manSuppProps);explicitManSuppProps(ManSuppProps&manSuppProps);~ManSuppProps();private:QVector3Dm_sup

c++ - 为什么 std::pair 在赋值中调用显式构造函数

考虑以下代码:#include#includestructBase{intbaseint;};structDer1:Base{intder1int;Der1():der1int(1){}explicitDer1(constBase&a):Base(a),der1int(1){std::cerrstructMyPair{Tfirst;Usecond;};intmain(){Der1d1;Der2d2;std::pairp1;std::pairp2;p1=p2;//ThiscompilessuccessfullyMyPairmp1;MyPairmp2;mp1=mp2;//Thiswillr

c++ - 没有参数的 QDialog 显式构造函数 - 如何正确使用?

我在派生类中遇到过这种情况,但它与QDialog基类相同:当我这样做QDialogdialog();dialog.exec();编译器提示J:\...\mainwindow.cpp:-1:Inmemberfunction'voidMainWindow::on_viewButton_pressed()':J:\...\mainwindow.cpp:72:Fehler:requestformember'exec'in'dialog',whichisofnon-classtype'QDialog()'这与正在使用的构造函数有关,因为当我这样做时QDialogdialog(0);dialog.

c++ - 在 std::auto_ptr 的构造函数中使用 `explicit` 关键字有什么原因吗?

这是用于在VS2008编译器中从标准指针构造std::auto_ptr对象的构造函数。templateclassauto_ptr{public:explicitauto_ptr(_Ty*_Ptr=0)_THROW0():_Myptr(_Ptr){}private:_Ty*_Myptr;};explicit是否有任何特殊原因?上面使用关键字?换句话说,为什么我不能初始化auto_ptr与std::auto_ptrptr=newClassA;? 最佳答案 因为否则您可能会无意中执行以下操作:voidfoo(std::auto_ptrp)

c++ - 为什么调用了错误的 ctor?

我有按预期工作的代码:EscapedStringes("Abc&def");EscapedStringes2("");es2=es;//es2==Abc%26def以及未按预期工作的代码:EscapedStringes("Abc&def");EscapedStringes2=es;//es==Abc%2526def在第二种情况下,即使es是EscapedString,也会调用CTOR2而不是CTOR3。EscapedStringes(EscapedString("Abc?def"));做正确的事,但我似乎无法在CTOR3上设置断点,所以我不确定它是否正常工作,或者代码已被优化掉或意外工

c++ - 为什么某些隐式类型转换在一台机器上是安全的而不是在另一台机器上?我怎样才能防止这个跨平台问题?

我最近在我的代码中发现了一个错误,我花了几个小时来调试。问题出在定义为的函数中:unsignedintfoo(unsignedinti){longintv[]={i-1,i,i+1};...returnx;//evaluatedbythefunctionbutnotessentialhowforthisproblem.}v的定义在我的开发机器(ubuntu12.0432位,g++编译器)上没有造成任何问题,其中unsignedint被隐式转换为longint,因此负值得到了正确处理。在不同的机器上(ubuntu12.0464位,g++编译器)但是这个操作并不安全。当i=0时,v[0]没

c++ - 如何在命名空间中导入 C++ 类的 dll

我阅读了一些文档,其中提供了与C兼容的函数的简单示例。__declspec(dllexport)MyFunction();我对此很满意。我写了一个小应用程序使用这个dll的功能。我使用了显式链接LoadLibrary()函数。C风格的函数可以毫无问题地工作。但是当我把我的课写成namespaceDllTest{classTest{public:__declspec(dllexport)Test();__declspec(dllexport)voidFunction(int);__declspec(dllexport)intgetBar(void);private:intbar;};}#