草庐IT

CONSTRUCTOR

全部标签

c++ - 为什么 `return {};` 不适用于 `std::forward_list` ?

我的编译器是clang3.4,完全支持C++14和std::forward_list。#includestructA{A(){}explicitA(initializer_list){}};Af1(){returnA();//OK}Af2(){return{};//OK}typedefstd::forward_listT;Tf3(){returnT();//OK}Tf4(){//error:convertingto'T{akastd::forward_list}'frominitializer//listwoulduseexplicitconstructor'std::forward_

c++ - 使用 () 创建类的实例

我有一个问题:在C++中使用ClassNameinstance()创建类的实例时使用什么构造函数?例子:#includeusingnamespacestd;classTest{private:Test(){cout谢谢! 最佳答案 棘手!您会期望编译失败,因为默认构造函数是私有(private)的。但是,它会编译并且不会创建任何内容。原因是什么?Testinstance_1();...只是一个函数声明!(它返回Test并且什么都不做。) 关于c++-使用()创建类的实例,我们在Stack

c++ - vector push_back 调用 copy_constructor 不止一次?

我对vectorpush_back的行为方式有点困惑,在下面的代码片段中,我希望复制构造函数只被调用两次,但输出表明并非如此。是否是导致此行为的vector内部重组。输出:InsidedefaultInsidecopywithmy_int=0Insidecopywithmy_int=0Insidecopywithmy_int=1classMyint{private:intmy_int;public:Myint():my_int(0){coutmyints;Myintx;myints.push_back(x);x.set(1);myints.push_back(x);

c++ - 如何在模板中初始化为零/空

在编写模板时,我想将变量初始化为数据类型为零或空的值。如果我将它设置为0x00,它会作为任何类型的零/NULL吗?例如这是模板声明template...TA=0x00;现在如果我定义类型T=>std::string的实例,上面的语句用作NULL?“int”和“unsignedint”呢?对于两者,它都用作“0”? 最佳答案 使用ValueInitialization:TA=T();//beforeC++11TA{};//C++11andlaterTheeffectsofvalueinitializationare:1)ifTisac

c++ - c++ block 内局部变量的存储分配

我想知道编译器在什么时候为block内的局部变量分配存储空间。goto和switch如何跳过构造函数?:classTree{/*...*/}...voidfoo(inti){if(i虽然上面的代码不适用于用户定义的对象,但如果我用内置对象替换它们,它就可以工作。这是为什么?编辑:内置对象,如int、char等。我得到的错误(ubuntu上的g++4.5):jumpPastConstructor.c++:Infunction‘voidfoo(int)’:jumpPastConstructor.c++:26:3:error:jumptolabel‘label’jumpPastConstru

c++ - 不明确的构造函数调用

我正在尝试创建一个简单的日期类,但我在主文件中收到一条错误消息:“重载Date()的调用不明确。”我不确定为什么因为我认为只要我的构造函数有不同的参数,我就可以了。这是我的代码:头文件:#ifndefDATE_H#defineDATE_Husingstd::string;classDate{public:staticconstintmonthsPerYear=12;//numofmonthsinayrDate(int=1,int=1,int=1900);//defaultconstructorDate();//usessystemtimetocreateobjectvoidprint(

C++ copy-construct 构造和赋值问题

这是“C++Gotchas”一书第56项的摘录:It'snotuncommontoseeasimpleinitializationofaYobjectwrittenanyofthreedifferentways,asiftheywereequivalent.Ya(1066);Yb=Y(1066);Yc=1066;Inpointoffact,allthreeoftheseinitializationswillprobablyresultinthesameobjectcodebeinggenerated,butthey'renotequivalent.Theinitializationof

c++ - 具有初始值的类构造

我是C++的新手,以及类的整个概念-我仍在阅读一本书来尝试学习。我正在阅读的书说,当我构造一个类时,我可以通过这样做来分配默认值:classfoo{public:foo(charc,inti);private:charexampleChar;intexampleInt;};foo::foo(charc,inti):exampleChar(c),exampleInt(i){}此代码(对我而言)看起来非常困惑,并且不遵循我在其他语言中习惯的规则。我的问题是,执行上述操作和执行此操作(在下面,我个人认为看起来更干净)之间有什么区别?foo::foo(charc,inti){exampleCh

c++ - 为什么不为给定的转换运算符调用构造函数?

structA{};structB{B(A*pA){}B&operator=(A*pA){return*this;}};templatestructWrap{T*x;operatorT*(){returnx;}};intmain(){Wrapa;BoB=a;//error:conversionfrom‘Wrap’tonon-scalartype‘B’requestedoB=a;//ok}当oB那么构造为什么B::B(A*)未为Wrap::operatorT()调用?[备注:B::operator=(A*)为Wrap::operatorT()调用在下一条声明中]

c# - 使用正确的构造函数创建 View Controller 的实例

教程中UseSplitViewtoShowtwoControllers您以这种方式创建ViewController的实例:masterView=newMasterViewController();因此你需要这样的构造器:publicMasterViewController():base()我目前的问题是我的MasterViewController没有加载。如果我运行该应用程序,会显示一个空表,但我的表中应该有一些自定义项和数据。现在我正在使用自定义单元格。在教程中Part2-PopulatingaTablewithData明确指出:Beaware,whenusingthenewreus