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
explicit
keyword 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
. Theexplicit
keyword 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的自增问题
程序执行结果是0,,,因为count永远是0
- Spring学习记录(一)
1.Spring简介 Spring是一个轻量级的java开发框架.框架的主要优势之一就是分层架构,Spring使用基本的JavaBean,不仅限于服务器的开发.从简单性,可测试性和松耦合的角度而言,任 ...
- Java控制语句例题,for循环语句,if条件语句等,Scanner类与Random类,Math.max()方法
例题:编写程序,生成5个1至10之间的随机整数,并打印结果到控制台 import java.util.Random;class demo09 { public static void main(Str ...
- 正则表达式-基础知识Review
正则表达式(Regular Expression)是计算机科学的一个概念. 正则表达式使用单个字符窜来描述.匹配一系列符合某个句法规则的字符窜. 在很多文本编辑器里, 正则表达式通常用来被检索替换哪些 ...
- nuxt.js express模板项目虚拟目录部署问题汇总
声明环境 反向代理:nginx或者iis的ARR 模板项目:nuxt-express 部署环境:windows 经过了一段时间在windows环境部署项目来看,关于虚拟目录的问题汇总如下, 发布场景假 ...
- 01.VUE学习一
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 用iTerm快速链接远程服务器
通常情况下,iTerm2访问远程Linux使用ssh ssh <用户名>@<ip> 然后输入访问的密码即可.当然还有的时候需要指定访问端口. ssh -p <端口号> ...
- Codeforces Round #435 (Div. 2) B (二分图) C(构造)
B. Mahmoud and Ehab and the bipartiteness time limit per test 2 seconds memory limit per test 256 me ...
- Django--源码安装
1.安装setuptools cd /usr/src tar zxf setuptools-18.3.2.tar.gz cd setuptools-18.3.2/ python setup.py bu ...
- cakephp中使用 find('count')方法
对于find('count',array('group'=>'user_id')); Model.php中这样描述: /** * Handles the before/after filter ...