草庐IT

default-constructor

全部标签

c++ - 为什么我的编译器无法识别 "Bond() = default;"?

请看这段代码classBond{public:Bond(intpayments_per_year,intperiod_lengths_in_months);Bond()=default;private:constintpayments_per_year;constintperiod_length_in_months;};intmain(){Bondb;//Errorhere}尝试编译时出现错误:errorC2280:'Bond::Bond(void)':attemptingtoreferenceadeletedfunction".这不是违反“3规则”,因为我已经添加了默认构造函数。为什

c++ - 为什么我的编译器无法识别 "Bond() = default;"?

请看这段代码classBond{public:Bond(intpayments_per_year,intperiod_lengths_in_months);Bond()=default;private:constintpayments_per_year;constintperiod_length_in_months;};intmain(){Bondb;//Errorhere}尝试编译时出现错误:errorC2280:'Bond::Bond(void)':attemptingtoreferenceadeletedfunction".这不是违反“3规则”,因为我已经添加了默认构造函数。为什

c++ - C++ 在空类中编写和调用什么函数?

在EffectiveC++一书中,我看到了下面这段话:Asaresult,ifyouwriteclassEmpty{};it'sessentiallythesameasifyou'dwrittenthis:classEmpty{public:Empty(){...}Empty(constEmpty&rhs){...}~Empty(){...}Empty&operator=(constEmpty&rhs){...}//copyassignmentoperator};Thefollowingcodewillcauseeachfunctiontobegenerated:Emptye1;Emp

c++ - C++ 在空类中编写和调用什么函数?

在EffectiveC++一书中,我看到了下面这段话:Asaresult,ifyouwriteclassEmpty{};it'sessentiallythesameasifyou'dwrittenthis:classEmpty{public:Empty(){...}Empty(constEmpty&rhs){...}~Empty(){...}Empty&operator=(constEmpty&rhs){...}//copyassignmentoperator};Thefollowingcodewillcauseeachfunctiontobegenerated:Emptye1;Emp

c++ - 删除默认类构造函数有什么意义?

我正在准备我的CPP考试,其中一个问题是:您可以删除默认类构造函数吗?如果可以,这样做的原因是什么?好的,很明显你可以做到:classMyClass{public:MyClass()=delete;};但我不明白你为什么要这样做? 最佳答案 考虑以下类:structFoo{inti;};这个类是一个聚合,你可以创建一个包含所有这三个定义的实例:intmain(){Foof1;//iuninitializedFoof2{};//iinitializedtozeroFoof3{42};//iinitializedto42}现在,假设您不

c++ - 删除默认类构造函数有什么意义?

我正在准备我的CPP考试,其中一个问题是:您可以删除默认类构造函数吗?如果可以,这样做的原因是什么?好的,很明显你可以做到:classMyClass{public:MyClass()=delete;};但我不明白你为什么要这样做? 最佳答案 考虑以下类:structFoo{inti;};这个类是一个聚合,你可以创建一个包含所有这三个定义的实例:intmain(){Foof1;//iuninitializedFoof2{};//iinitializedtozeroFoof3{42};//iinitializedto42}现在,假设您不

c++ - 为什么我们需要使用 virtual ~A() = default;而不是 C++11 中的虚拟 ~A() {}?

在堆栈溢出帖子中CheckingtheobjecttypeinC++11,我有意见:InC++11you'llactuallywanttodovirtual~A()=default;Otherwise,you'lllosetheimplictmoveconstructors.virtual~A()=default;是干什么用的?virtual~A(){}为何会丢失隐式移动构造函数? 最佳答案 评论不正确。两者:virtual~A()=default;和virtual~A(){}被用户声明。如果析构函数是用户声明的,则隐式移动成员将被

c++ - 为什么我们需要使用 virtual ~A() = default;而不是 C++11 中的虚拟 ~A() {}?

在堆栈溢出帖子中CheckingtheobjecttypeinC++11,我有意见:InC++11you'llactuallywanttodovirtual~A()=default;Otherwise,you'lllosetheimplictmoveconstructors.virtual~A()=default;是干什么用的?virtual~A(){}为何会丢失隐式移动构造函数? 最佳答案 评论不正确。两者:virtual~A()=default;和virtual~A(){}被用户声明。如果析构函数是用户声明的,则隐式移动成员将被

javascript - __proto__ 与 constructor.prototype 有何不同?

functionGadget(name,color){this.name=name;this.color=color;}Gadget.prototype.rating=3varnewtoy=newGadget("webcam","black")newtoy.constructor.prototype.constructor.prototype.constructor.prototype它总是返回rating=3的对象。但如果我执行以下操作:newtoy.__proto__.__proto__.__proto__链最终返回null。同样在InternetExplorer中,如果没有__p

javascript - __proto__ 与 constructor.prototype 有何不同?

functionGadget(name,color){this.name=name;this.color=color;}Gadget.prototype.rating=3varnewtoy=newGadget("webcam","black")newtoy.constructor.prototype.constructor.prototype.constructor.prototype它总是返回rating=3的对象。但如果我执行以下操作:newtoy.__proto__.__proto__.__proto__链最终返回null。同样在InternetExplorer中,如果没有__p