草庐IT

constructor_arguments

全部标签

c++ - 在 Constructor works 中将临时变量传递给引用 arg。但不是一般的功能。为什么?

这个问题在这里已经有了答案:Defaultconstructorwithemptybrackets(9个回答)关闭7年前。考虑以下代码。在这里,即使构造函数是A(B&b),Aa(B())也会编译;但是print(B())不起作用。但是print也被声明为print(B&b);为什么会出现这种不一致?#includeusingnamespacestd;classB{public:charb;};classA{public:Bb;A(B&b);A(){}};A::A(B&b){this->b=b;}voidprint(B&b){}intmain(){print(B());Aa(B());}

C++ 模板复制构造函数,编译器说 "passing const as this argument discards qualifiers"

我正在尝试创建动态矩阵的模板类。凭借我目前对C++的了解,我设法解决了一些问题,但我被复制构造函数和重载operator=;困住了。换句话说,我无法创建对象的拷贝。在我看来这应该可行,但我的编译器friend告诉我有1个错误:错误:将“constMatrix”作为“intMatrix::getElement(int,int)[withT=int]”的“this”参数传递会丢弃此行的限定符[-fpermissive]:m[i][j]=original.getElement(i,j);当我想创建一个对象时:Matrixm=Matrix(3,3);我的模板类在这里:templateclass

c++ - (C++) 错误 : 'invalid_argument' was not declared in this scope

我正在使用EclipseC/C++和MinGW编译器。我已将标志-std=c++11添加到项目属性中C/C++Build下的MiscellaneousGCCCCompilerSettings中。我知道这可能是一件简单的事情,但我无法解决此错误。Date.h#includeusingnamespacestd;classDate{public:Date(intm=1,intd=1,inty=1900);voidsetDate(int,int,int);private:intmonth;intday;intyear;staticconstintdays[];};日期.cpp#include#

c++ - 警告 C4244 : 'argument' : conversion from 'SIZE_T' to 'DWORD' , 可能丢失数据

我需要在我的代码中有一组重载函数,但我得到了转换wanrings。这是一个测试代码:#includewindows.hvoidf(DWORDarg){...}//voidf(SIZE_Targ){}voidmain(void){DWORDdword=0;SIZE_Tsize_t=dword;f(size_t);}编译器给出警告:test.cpp(11):warningC4244:'argument':conversionfrom'SIZE_T'to'DWORD',possiblelossofdata如果我取消注释voidf(SIZE_Targ)我得到test.cpp(5):errorC

c++ - C++ 代码错误 "expected constructor, destructor, or type conversion before ‘(’ token ”和 "no matching function for call to ..."

真正尝试解决错误,仔细检查所有内容。请帮忙。c++新手,请多关照。头文件(.h)#ifndefGUARD_Optimized_quick_sort_h#defineGUARD_Optimized_quick_sort_h#include#include#includeusingnamespacestd;templateclassoptimized_quick_sort{public:optimized_quick_sort(vectorarray){this->array=array;}optimized_quick_sort(listarray){vectortemp(array.b

C++ 单例用法 : compiler complains about private constructor

我知道有一百万个关于单例的问题和答案,但我似乎无法找到解决方案。所以冒着反对票的风险,这是我的问题:我想使用AndreiAlexandrescu的现代C++设计中的单例实现:标题:classSingleton{staticSingleton&Instance();private:Singleton(){};Singleton(constSingleton&){};Singleton&operator=(constSingleton&){};~Singleton(){};};实现:#include"s.hh"Singleton&Singleton::Instance(){staticSi

c++ - 与 move 构造函数混淆 : unable to call move constructor

我一直难以理解C++中的move构造函数。我用默认构造函数、复制构造函数、move构造函数和析构函数制作了一个简单的类。此外,我定义了一个具有两个重载的函数,一个接受对该类的引用,一个接受对该类的右值引用。我的测试代码如下。#includeclassc{public:c(){std::cout我得到的输出不是我所期望的。以下是我从此代码获得的输出。defaultconstructorcopyconstructorpassedbyreferencedefaultconstructorpassedbyrvaluereferencedestructor除了第3行,我能理解所有行的输出。在第3

c++ - 如何在不使用异常的情况下检查 constructor() 中的失败?

我正在处理的所有类都有Create()/Destroy()(或Initialize()/Finalized())方法。Create()方法的返回值是bool,如下所示。boolMyClass::Create(...);所以我可以通过返回值来检查实例的初始化是否成功。如果没有Create()/Destroy(),我可以在constructor()和destroy()中做同样的工作,但我无法解决以下问题。谁能帮帮我?提前致谢。我不能使用异常(exception),因为我的公司不喜欢它。classFoo{private:AnotherClassa;public:Foo(){if(a.Init

c++ - 为什么这里调用的是 Copy Constructor 而不是普通的 Constructor 和重载的赋值运算符?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:IsthereadifferenceinC++betweencopyinitializationanddirectinitialization?CopyconstructorsandAssignmentOperators我有一个C类,我在其中重载了Normal、复制构造函数和赋值运算符以打印被调用内容的踪迹。我写了以下代码来测试什么时候被调用?Cc1;-->NormalConstuctor..//understoodFineCc2;c2=c1;-->Normalconstructor+assignmentop

c++ - 继承层次结构 : Constructor & Destructor execution sequence

在这里http://www.parashift.com/c++-faq-lite/multiple-inheritance.html[25.14]节说Theveryfirstconstructorstobeexecutedarethevirtualbaseclassesanywhereinthehierarchy.我尝试使用以下程序验证它:A(purevirtual)|B|C(virtual)/\(virtual)ED\/F|G(purevirtual)|H每个类都有一个c'tor和virtuald'tor。输出如下:ABCEDFGH~H~G~F~D~E~C~B~APressanyke