C++ essentials 之 explicit constructor
这篇博客的源起是下面的一段代码
#include <bits/stdc++.h>
using namespace std;
int main(){
priority_queue<long long> que;
// some operations on que
que = {};
// some operations on que
return 0;
}
其中 que = {} 相当于 que.clear();(std::priority_queue 并没有 clear() 方法)。以前我清空 priority_queue 用的是
while(!que.empty()){
que.pop();
}
然而编译器对这个语句给出了一个警告:
(g++ 6.3.0:g++ -Wall -std=c++14)
In function 'int main()':
7:12: warning: converting to 'std::priority_queue<long long int>' from initializer list would use explicit constructor 'std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, _Sequence&&) [with _Tp = long long int; _Sequence = std::vector<long long int, std::allocator<long long int> >; _Compare = std::less<long long int>]'
que = {};
^
7:12: note: in C++11 and above a default constructor can be explicit
概念
implicit class-type conversion
Every constructor that can be called with a single argument defines an implicit conversion to a class type. Such constructors are sometimes referred to as conversion constructors.
explicit constructors
We can prevent the use of a constructor in a context that requires an implicit conversion by declaring the constructor as
explicit.The
explicitkeyword is meaningful only on constructors that can be called with a single argument. Constructors that require more arguments are not used to perform a conversion, so there is no need to designate such constructors asexplicit. Theexplicitkeyword is used only on the constructor declaration inside the class. It is not repeated on a definition made outside the class body.One context in which implicit conversions happen is when we use the copy form of initialization (with an
=). An explicit constructor cannot be used with the copy form of initialization; it can be used only with the direct form of initialization. Moreover, the compiler will not use this constructor in an automatic conversion.
C++ essentials 之 explicit constructor的更多相关文章
- 错误:Implicit super constructor xx() is undefined for default constructor. Must define an explicit constructor
错误:Implicit super constructor xx() is undefined for default constructor. Must define an explicit con ...
- Eclipse错误:Implicit super constructor ClassName is undefined for default constructor. Must define an explicit constructor
public class Test01 { private String name; private int age; public Test01(String name){ this.name = ...
- C++ explicit constructor/copy constructor note
C++:explict 作用显示声明构造函数只能被显示调用从而阻止编译器的隐式转换,类似只能用()显示调用,而不能=或者隐式调用 #include <iostream> #include ...
- Implicit super constructor xx() is undefined for default constructor. Must define an explicit constructor
错误:Implicit super constructor xx() is undefined for default constructor. Must define an explicit c ...
- explicit constructor(显示构造函数)
按照默认规定,只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应的数据类型的数据转换为该类对象,如下所示: class String { String(const char* p) //用C ...
- 子类继承父类时JVM报出Error:Implicit super constructor People() is undefined for default constructor. Must define an explicit constructor
当子类继承父类的时候,若父类没有定义带参的构造方法,则子类可以继承父类的默认构造方法 当父类中定义了带参的构造方法,子类必须显式的调用父类的构造方法 若此时,子类还想调用父类的默认构造方法,必须在父类 ...
- Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor
转自:https://blog.csdn.net/u013125680/article/details/43887987 解决方案:把java的类库加载进去,在工程上右键选择属性->Java B ...
- Implicit super constructor Array() is undefined for default constructor. Must define an explicit constructor
因为你的父类已经创建了一个带参的构造函数并且父类中没有无参的构造函数,此时编译器不会为你调用默认的构造函数, 所以子类在继承父类的时候需要在自己的构造函数中显式的调用父类的构造函数,这样才能确保子类在 ...
- 第8课 列表初始化(3)_防止类型收窄、explicit关键字
1. 防止类型收窄 (1)类型收窄:指的是导致数据内容发生变化或者精度丢失的隐式类型转换. (2)类型收窄的几种情况: ①从浮点数隐式转换为整型数,如int i=2.2; ②从高精度浮点数隐式转换为低 ...
随机推荐
- Java设计模式学习——简单工厂
一. 定义与类型 定义:有工程对象决定创建出哪一种产品类的实例 类型:创建型,但不属于GOF23中设计模式 二. 适用场景 工厂类负责创建的对象比较少 客户端(应用层)只知道传入工厂类的参数,对于如何 ...
- 绑定Ligerui中的ligerComboBox二级联动
$.ajax({ url: "HRHandler.ashx", data: "bz=getDepartData", cache: false, type: &q ...
- 逗塔战争TD新人入门图文攻略
逗塔战争TD新人入门图文攻略 <逗塔战争TD>是一张基于DOTA改编的塔防TD,很多玩家都很喜欢这张图,新手玩家怎么快速上手这张图呢?这张图的玩法和基本规则并不难,下面就为大家带来新人 ...
- NodeJS--exports和module.exports
继续迁移印象笔记中记录相关笔记,其实工作中遇到的很多问题当时解决了,后期就忘记了,多记录还是很有用的,好记性不如烂笔头嘛,以后要养成好习惯. NodeJS中 require 用来加载代码,而 expo ...
- 1- vue django restful framework 打造生鲜超市
Vue+Django REST framework实战 使用Python3.6与Django2.0.2(Django-rest-framework)以及前端vue开发的前后端分离的商城网站 项目支持支 ...
- 3.Cisco Packet Tracer中关于交换机端口安全的设置
本次实验将在这幅拓扑图的基础上完成 我们会对pc0在交换机上进行mac地址绑定,pc1访问时则交换机断开端口 1.为pc机配置ip地址 pc0:192.168.1.1 pc1:192.168.1.2 ...
- Linux IO调度方法
目录 I/O调度的4种算法 I/O调度程序的测试 ionice IO调度器的总体目标是希望让磁头能够总是往一个方向移动,移动到底了再往反方向走,这恰恰就是现实生活中的电梯模型,所以IO调度器也被叫做电 ...
- centos7上mysql8.0rpm方式安装
首先是下载图解 1.首先卸载centos7中自带的mariadb rpm -qa|grep mariadb //查询出来已安装的mariadb rpm -e --nodeps 文件名 //卸载mari ...
- java中的继承 (2013-10-11-163 写的日志迁移
继承:为了解决代码重用 定义: 子类通过继承父类,可以调用父类中非私有的属性和方法,达到重用的目的,通过关键字extends实现: ################以下为代码演示: class A ...
- Python 格式化
数字前面补0 字符型: print('23'.zfill(5)) 数字型: print('%011d' % 124) 日期与str互转: datetime 转 str str_date = datet ...